Service Template syntax

I am trying to set up an automation that will toggle a switch based on an mqtt payload. I am using the service_template but can’t seem to get the syntax correct. Can someone please look at what I have and let me know if you see any issues?

  • service_template: “{% if (’{{trigger_payload}}’,‘on’) %}switch.turn_on{% else %}switch.turn_off{% endif %}”

Thank you!

Try:

service_template: "{% if trigger.payload == 'on' %}switch.turn_on{% else %}switch.turn_off{% endif %}"

Perfect! That is exactly what I was trying to do. Thank you very much for your help, its working great!

1 Like