Automation MQTT trigger.payload how to check string value?

Hi
I’m a noobie (and first posting) and am just trying to get an automation to work where by an inbound MQTT event causes a sonoff to turn on via a MQTT published topic as per below:

- id: '1541745678590'
  alias: GatebellLight
  trigger:
  - platform: mqtt
    topic: fieldcottage/gatesensors/activated
  condition: []
  action:
  - data_template:
      payload: >-
        {% if trigger.payload|string == 'ON' %}
          'ON'
        {% else %}
          'OFF'
        {% endif %}
      topic: cmnd/sonoff01/power
    service: mqtt.publish

While the automation works, the issue is that the payload sent to the sonoff is always ‘OFF’, I’m very sure the payload with the inbound topic is just ‘ON’ (in capitals). (I don’t wish to set the outbound payload to inbound payload as eventually I will add more logic later).

Maybe I’m doing it all wrong so any advice very welcomed.

Sorry if formatting not right - I’ve pressed the </> button but nothing seemed to happen.

You need to format your code properly, use the blue link at the top of the post. Edit your original post. Do not copy from your original post and paste into a new post. The original formatting from your yaml will be lost if you do.

Select the text to be formatted first, then click the </> button.

Another way is to simply type three backquotes ``` before and after the block of text to be formatted.

EDIT
Ninja’d by petro!

Cheers the ``` works a treat much easier. Thanks

Yep, that’s my goto. Easier to edit as well because you don’t need to load the yaml with 4 extra spaces.

Try this instead, you may be getting spaces after 'ON ' instead of just 'ON':

- id: '1541745678590'
  alias: GatebellLight
  trigger:
  - platform: mqtt
    topic: fieldcottage/gatesensors/activated
  condition: []
  action:
  - data_template:
      payload: >-
        {% if trigger.payload.strip() == 'ON' %}
          'ON'
        {% else %}
          'OFF'
        {% endif %}
      topic: cmnd/sonoff01/power
    service: mqtt.publish

How are you confirming the automation is publishing to cmnd/sonoff01/power? Are you using MQTT.fx or some other MQTT client to monitor the topic and its payload?

BTW, I have no experience with Tasmota but according to Tasmota’s Wiki, payload is on or off. I don’t know if its case-sensitive because you’re sending ON and OFF.

Thanks @123 you made me really really check the topics were working (I’m using MQTT lens) and the issue was simply I was setting the payload to ‘ON’ and it should have been ON (no quotes). Doh!