Trigger - Only change state if different

Hi All

I have now tried so many options… with no luck. Can someone please assist?

I want the switch to change state ONLY if it’s different

value = on or off
item = part of input_boolean name

I keep on getting errors when I do a Configuration check.

  - alias: Receive Message
    trigger:
    - platform: webhook
      webhook_id: !secret the_key
    condition: template
    value_template: >
      {{ input_boolean.alarm_{{ trigger.json.item }}.state != {{ trigger.json.value }} }}
    action:
      service: >
        input_boolean.turn_{{ trigger.json.value }}
      data:    
        entity_id: >
          input_boolean.alarm_{{ trigger.json.item }}          

Thank you

You didn’t say what the error was so I’m making an assumption (but an educated one)

try this:

value_template: >
  {% set item = trigger.json.item %}
  {{ states.input_boolean.alarm_~item~.state != trigger.json.value }}

I’m not sure it will work but I think it should.

Thank you finity

It still does not work. The error message is still:

Invalid config for [automation]: Expected a dictionary @ data[‘condition’][0]. Got None extra keys not allowed @ data[‘value_template’]. Got None.

That helped. Your indentation is off and missing some stuff

try this:

- alias: Receive Message
    trigger:
    - platform: webhook
      webhook_id: !secret the_key
    condition: 
      condition: template
      value_template: >
        {% set item = trigger.json.item %}
        {{ states.input_boolean.alarm_~item~.state != trigger.json.value }}
    action:
      service: >
        input_boolean.turn_{{ trigger.json.value }}
      data:    
        entity_id: >
          input_boolean.alarm_{{ trigger.json.item }}

Thank you, but now I get this error:

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘.’) for dictionary value @ data[‘condition’][0][‘value_template’]. Got None.

Hi finity

      value_template: >
        {% set itm = trigger.json.item %}
        {{ states("input_boolean.alarm_" + itm +".state") != trigger.json.value }}

Gives no errors - Now I just have to check if it actually works :slight_smile: