AppDaemon / appdaemon

:page_facing_up: Python Apps for Home Automation
Other
826 stars 418 forks source link

mqtt retained messages not send to app on appdaemon startup #2006

Open stablestud opened 2 months ago

stablestud commented 2 months ago

What happened?

First of all, thanks for creating AppDaemon, my goto for HA automations!

I was trying to iterate over the mqtt broker stored/retained messages in my app via the topic 'homeassistant/#' on appdaemon startup. However retained messages are never delivered to it but only updates after the app has started.

When running appdaemon in DEBUG log level I can see on startup that it's receiving the retained messages from the broker, but it doesn't forwards them to my app.

Which is sad as I was trying to append the force_update: True value to all retained messages.

Here is my appdaemon.yaml:

  appdaemon:
  time_zone: !secret timezone
  latitude: !secret latitude
  longitude: !secret longitude
  elevation: !secret elevation
  plugins:
    homeassistant:
      type: hass
      namespace: homeassistant
      ha_url: !secret ha_url
      token: !secret ha_token
    mqtt:
      type: mqtt
      client_id: !secret mqtt_client
      namespace: mqtt
      client_host: !secret mqtt_host
      client_port: 1883
      client_user: !secret mqtt_user
      client_password: !secret mqtt_password
      client_topics:
        - "homeassistant/#"
http:
  url: http://127.0.0.1:5050
api:
admin:
hadashboard:

apps/apps.yaml:

MqttForceUpdate:
  module: mqttforceupdate
  class: MqttForceUpdate

This is similar to https://github.com/AppDaemon/appdaemon/issues/571 but in that issue the author seemed to have atleast received the retained messages on appdaemon startup

Version

4.2.2

Installation type

Python virtual environment

Relevant log output

No response

Relevant code in the app or config file that caused the issue

mport json
import re

import mqttapi as mqtt

'''
This class makes all mqtt entities to trigger an state update on every new incoming payload.
This is required for statistics in influxdb and grafana,
as without, only actual data changes are forwarded to influx,
but if the last send data is out-of the current graph timespan no data will be shown.
Instead make every sensor update to be written to the influxdb
'''
class MqttForceUpdate(mqtt.Mqtt):

    def initialize(self):
        self.set_log_level("DEBUG")
        self.set_namespace("mqtt")
        self.listen_event(self.mqtt_message_received_event, "MQTT_MESSAGE")

    def mqtt_message_received_event(self, event_name, data, cb_args):
        try:
            topic = data["topic"]
            payload = json.loads(data["payload"])
            if not "force_update" in payload:
                self.mqtt_force_update(topic=topic, payload=payload)
        except (json.JSONDecodeError, KeyError) as err:
            print(err)

    def mqtt_force_update(self, **cb_args):
        topic = cb_args["topic"]
        payload = cb_args["payload"]
        payload["force_update"] = True
        self.log(f"Forcing update for '{topic}'")
        self.mqtt_publish(topic=topic, payload=payload, retain = True)

Anything else?

No response

stablestud commented 2 months ago

PS: self.mqtt_publish(topic=topic, payload=payload, retain = True) doesn't seem to work either, as nothing is published when triggered, but this is for another issue... maybe my configuration is just simply wrong?

EDIT: fixed it, I had to convert the payload back to a string for it to work