Hi,
I’d like to send an mqtt message with the state of an input_datetime field.
So everytime the time changes send a message.
Any advice on how to achieve this would be very welcome
Hi,
I’d like to send an mqtt message with the state of an input_datetime field.
So everytime the time changes send a message.
Any advice on how to achieve this would be very welcome
trigger:
platform: state
entity_id: input_datetime.your_input_datetime_here
action:
service: mqtt.publish
topic: your/topic/here
payload_template: {{ states('input_datetime.your_input_datetime_here') }}
qos: 0 # or 1 or 2
retain: false # or true if you need it
Thanks! Ill try it tomorrow
Info for future people with the same/similar question:
my main issue here was with the mqtt publish, regular payload worked but couldn’t get payload_template working, for payload template sending the state is done as follows:
payload_template: {{ states.input_boolean.vent_automation_enable.state }}
if you leave the .state away on the end you get all info of the entity. ‘vent_automation_enable’ is the entity name in my case.
this can be expanded by using strings:
payload_template: Enable {{ states.input_boolean.vent_automation_enable.state }}
In this case the payload would look like ‘Enable on’ or ‘Enable off’
and you can add multiple entities:
payload_template: Enable {{ states.input_boolean.vent_automation_enable.state
}} Start {{ states.input_datetime.start_vent.state }} Stop {{ states.input_datetime.stop_vent.state
}}
This was my end goal and results in a payload like this:
‘Enable off Start 23:00:00 Stop 05:00:00’
Is there any reason you chose to ignore my advice to use this template format?
{{ states('input_datetime.your_input_datetime_here') }}
There’s a warning about using the form {{ states.input_boolean.vent_automation_enable.state }}
on the templating document page you should read.
Avoid using
states.sensor.temperature.state
, instead usestates('sensor.temperature')
. It is strongly advised to use thestates()
,is_state()
,state_attr()
andis_state_attr()
as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).
Hi tom,
Because i couldn’t get it to work that way.
Always had errors using that format.
Might try again later tonight
Post what you tried and we can help you fix it.
For all the reasons tom_I mentioned, it’s preferable to use states()
so it’s to your advantage to learn how to use it correctly sooner than later.
Ok, so now I’m just testing with mqtt.publish service:
topic: /test
payload_template: {{ states('sun.sun') }}
qos: 0
retain: true
this gives the following error:
Try this,
payload_template: "{{ states('sun.sun') }}"
Thats it! Thanks for the correction
Then the document examples need editing, they show no quotes around the template. I’ve created a PR to fix it.