Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stablestud opened this issue May 4, 2024 · 1 comment
Open

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

stablestud opened this issue May 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@stablestud
Copy link
Contributor

stablestud commented May 4, 2024

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 #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 stablestud added the bug Something isn't working label May 4, 2024
@stablestud
Copy link
Contributor Author

stablestud commented May 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant