matthewwall / weewx-mqtt

upload data to mqtt broker
58 stars 31 forks source link

Home Assistant discovery metadata #26

Open ThomDietrich opened 2 years ago

ThomDietrich commented 2 years ago

Would you be willing to accept a PR with changes to enable Home Assistant auto discovery?

Out of coincidence I saw that the fork by @anastas78 already has an implementation for this, I wonder if he/she wants to open a PR!?

See https://github.com/anastas78/weewx-mqtt

Best!

matthewwall commented 2 years ago

happy to consider this. perhaps this could be done as a generic discovery, not specific to ha?

for example, the option might be something like this:

discovery = [True | False] # default false
discovery_topic = homeassistant/sensor/weewx # default None

and the docs would include an explanation of what the 'config' looks like for each key. or a reference to the ha discovery spec.

i'm a little anxious about sending a discovery message on every loop/archive. perhaps also have a mechanism to indicate how often the discovery info should be sent.

juandelarocha commented 1 year ago

Can you please explain how to enable Home Assistant auto discovery using the new mqtt.py developed? I have tried replacing the mqtt.py located in /usr/bin/weewx... but it doesn't work.

anastas78 commented 1 year ago

Can you please explain how to enable Home Assistant auto discovery using the new mqtt.py developed? I have tried replacing the mqtt.py located in /usr/bin/weewx... but it doesn't work.

The easiest way would be to download the updated branch from my repository i.e:

wget https://github.com/anastas78/weewx-mqtt/archive/refs/heads/ha-discovery-with-lwt.zip -O /tmp/weewx-mqtt.zip && \
cd /tmp && \
wee_extension --install weewx-mqtt.zip

and add the needed configuration in your weewx.conf

juandelarocha commented 1 year ago

Can you please explain how to enable Home Assistant auto discovery using the new mqtt.py developed? I have tried replacing the mqtt.py located in /usr/bin/weewx... but it doesn't work.

The easiest way would be to download the updated branch from my repository i.e:

wget https://github.com/anastas78/weewx-mqtt/archive/refs/heads/ha-discovery-with-lwt.zip -O /tmp/weewx-mqtt.zip && \
cd /tmp && \
wee_extension --install weewx-mqtt.zip

and add the needed configuration in your weewx.conf

Thanks for your help and implementation @anastas78. I have correctly downloaded, installed and configured the weewx.conf. I have been able to read out_temp by including the following in the configuration.yaml

`mqtt: sensor:

My question is, what should I expect by autodiscovery if I still need to define the variables in the configuration.yaml? Because if I not declare that, I don't receive anything. Maybe I am missing something?

anastas78 commented 1 year ago

Have you added the HA specific configuration in weewx.conf under the [[MQTT]] label ?

[[MQTT]]
    server_url = xxxx
    topic = weather
....... 
    ha_discovery = True
    ha_device_name = NAME_YOUR_DEVICE
    ha_discovery_topic = homeassistant/sensor/weewx/ 

so weewx-mqtt sends sensor data to "weather" topic and config data to "homeassistant/sensor/weewx/" (default topic if you have not changed your HA mqtt client configuration)

My question is, what should I expect by autodiscovery if I still need to define the variables in the configuration.yaml? Because if I not declare that, I don't receive anything. Maybe I am missing something?

That is the idea - you don't have to define anything in HA. All sensors published by weewx-mqtt should be configured directly by the mqtt client of HA though the "ha_discovery_topic" topic. It should create a new device with the name you configure under "ha_device_name" and a bunch of sensors - your weexw sensors.

CitizenRacer commented 1 year ago

Thank you @anastas78 for your variant. It works great.

@matthewwall I think this would be a great addition to the Weewx mqtt plugin. It makes Home Assistant integration so much easier!

timnis commented 1 year ago

Hi, just installed this and it partly works. Partly I mean I can see part of the sensors in HA (like below) image

But for example there is no rain_mm sensor. And on HA MQTT error log there is following error

2023-07-28 18:48:56.211 ERROR (MainThread) [homeassistant.components.mqtt.mixins] Error 'expected SensorDeviceClass or one of 'date', 'duration', 'enum', 'timestamp', 'apparent_power', 'aqi', 'atmospheric_pressure', 'battery', 'carbon_monoxide', 'carbon_dioxide', 'current', 'data_rate', 'data_size', 'distance', 'energy', 'energy_storage', 'frequency', 'gas', 'humidity', 'illuminance', 'irradiance', 'moisture', 'monetary', 'nitrogen_dioxide', 'nitrogen_monoxide', 'nitrous_oxide', 'ozone', 'pm1', 'pm10', 'pm25', 'power_factor', 'power', 'precipitation', 'precipitation_intensity', 'pressure', 'reactive_power', 'signal_strength', 'sound_pressure', 'speed', 'sulphur_dioxide', 'temperature', 'volatile_organic_compounds', 'volatile_organic_compounds_parts', 'voltage', 'volume', 'volume_storage', 'water', 'weight', 'wind_speed' for dictionary value @ data['device_class']' when processing MQTT discovery message topic: 'homeassistant/sensor/weewx/rain_mm/config', message: '{'name': 'rain_mm', 'unique_id': 'WMR300_rain_mm', 'device_class': 'mm', 'unit_of_measurement': 'mm', 'state_topic': 'weather/loop', 'value_template': '{{ value_json.rain_mm | float | round(1) }}', 'availability_topic': 'weather/availability', 'device': {'name': 'WMR300', 'manufacturer': 'weewx', 'model': 'WMR300', 'hw_version': 'weewx_version:4.10.2', 'sw_version': 'weewx-mqtt:0.24', 'identifiers': ['WMR300']}, 'platform': 'mqtt'}'

Any ideas how to fix this?

anastas78 commented 1 year ago

Hello @timnis, Just made some changes to the sensor types to match the evolution in HA sensor types and added the missing mm and mm_per_hour. (Thanks to @dcapslock for his review) Please check it #36

timnis commented 1 year ago

Hello @anastas78 , Thanks for update, now I see the rain 😃

jshatch commented 9 months ago

@anastas78 Though this is working for me, I find that having a new discovery message pop in on every loop might be causing issues (for instance I'm seeing the availability flap in openHAB). Can the discovery topic be linked to just report generation?

(edit: I blocked the HA discovery topic on my broker temporarily and sure enough the errors in the logs about going OFFLINE / UNKNOWN / ONLINE stopped. Adding that topic back into the aclfile immediately started the errors again.)

avfoto commented 7 months ago

@anastas78 When I run weewx before install your code I get... " TypeError: MQTTThread.init() got an unexpected keyword argument 'ha_discovery'"