Publish MQTT message from HA

Any idea why this does not work?

The template works just fine in dev tools.
Ideally I would like for HA to publish this message and also have a sensor for it but without me calling the publish service all the time to update it.
Is that possible?

Look where your first quote mark is, then pair that up with the next one. That is what is enclosed in quotes.

Use double quotes outside the template (or two single quotes) to prevent this happening.

As for automating it, create a template sensor that does the calculation.

Then create an automation with a state trigger for that sensor but use a null to: so it triggers on all state changes. Your action can publish the trigger.to_state variable.

Got it, was expecting it to work the same as on the template test page.
And is there a better way to publish this topic (it is on the HA mqtt addon) except for keep calling this service via an automation? Another MQTT device (not my HA server) is to consume it.
These values change every few seconds and I rather not have an automation firing constantly.

How often do you want to publish it then?

The values in the template update every 2 secs, but every 30/60 secs will be fine for the publish

Just use a time pattern trigger then, and in the actions publish the template sensor state.

https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger

  trigger:
    - platform: time_pattern
      minutes: "/1"
1 Like

That’s exactly what I did, thanks :smiley:

1 Like