cumulusmx / CumulusMX

The Cumulus MX weather program
GNU General Public License v3.0
84 stars 24 forks source link

MQTT dataupdate publishes every time the station updates, not when data in the topic changes #174

Closed sauntry closed 11 months ago

sauntry commented 1 year ago

I recently started using the mqtt dataupdate feature, and I was surprised when the topic was being published every few seconds. After looking at the code, the mqtt topics are published whenever the station updates, and not when the actual tags used in any particular topic has changed.

Request: only publish a topic when the contents change (not including any time/date webtag).

Bonus: I have this implemented and ready to submit a PR if you want this change.

mcrossley commented 1 year ago

Thanks, if you want to submit a PR I'll take a look.

dhover commented 1 year ago

If a greater structure needs to be published how to ignore the whole structure? For example: I'm using CumulusMX to send the weather data to Home Assistant. If for example you only send key/value pairs then everything goes fine. But what if I want to use the auto discovery functionality of Home Assistant. See https://www.home-assistant.io/integrations/mqtt/ The auto discovery function automatically creates the sensors in HA. If you don't use the auto discovery you have to create extra yaml code to create all the sensors.

To automatically import the mqtt values in HA you need a mqtt file in CumulusMX like this one:

{"topics":
[
  {
    "topic": "homeassistant/sensor/cumulusmx/temp/config",
    "retain": "true",
    "data": " {
                "name": "CumulusMX Temperature",
                "state_topic": "homeassistant/sensor/cumulusmx/interval/state",
                "value_template": "{{ value_json.temp }}"
              }
  },
  {
    "topic": "homeassistant/sensor/cumulusmx/hum/config",
    "retain": "true",
    "data": " {
                "name": "CumulusMX Humidity",
                "state_topic": "homeassistant/sensor/cumulusmx/interval/state"
                "value_template": "{{ value_json.hum }}"
              }
  },
  {
    "topic": "homeassistant/sensor/cumulusmx/interval/state",
    "retain": "false",
    "data": " {
                "temp": "<#temp>",
                "hum": "<#hum>"
              }"
  }
]
}

The best would be to send the first two complete topics only once (because they don't ever change). From the third topic send only the key/value pairs which have changed. Is that possible with the suggested change?

sauntry commented 1 year ago

Here's my 2 cents, since I opened this issue... I don't use Home Assistant, so perhaps I'm not completely understanding the issues you raise. The first seems to be concerning CumulusMX integration with HA through mqtt. That's perhaps best for another issue/request. This issue is concerning what triggers the update for individual topics for an Update.

I believe the second issue - only send the key/.value pairs that have changed - can already be addressed by having separate topics for each of the keys. You would have to account for that in each of the config topics (ie, by having state/temp and state/hum), and then a topic for each of those, along with my PR.

dhover commented 1 year ago

I understand the second issue could be addressed by dividing each key/value pair in a separate topic. In the first issue there are two topics which do not contain any CumulusMX web tags. Will those topics be send once or every time?

sauntry commented 1 year ago

My guess is that, with my PR, if you include a blank doNotTriggerOnTags (ie, "doNotTriggerOnTags": "") in each of those config topics, it will be sent once each time CumulusMX is started. You are welcome to grab the PR and give it a go.

mcrossley commented 11 months ago

Implemented in v3.26.0