Run automation if a state changes in a sensorr

Guys, i have this part of a string, the value (substring) 02 to 04 , can have a text like “00” or “01”

How can i run an automation based on that ? what i want is a toggle service if the text changes from 00 to 01 or from 01 to 00

how can i do that ?

{{ states.sensor.program.state[02:04] == '01' }}

this returns with the value True or False

automation:
  trigger:
    - platform: template
      value_template: "{{ states.sensor.program.state[02:04] == '01' }}"

ok, like this then? hmm, not sure if it will work

  trigger:
  - platform: template
    value_template: "{{ states.sensor.program.state[02:04] == '01' }}"
    from: '01'
    to: '00'
  - platform: template
    value_template: "{{ states.sensor.program.state[02:04] == '00 }}"
    from: '00'
    to: '01'
  action:
  - service: switch.turn_on
    entity_id: switch.xx

No. Look at my post.

yeah, but that doesnt work
i need a toggle service ONLY if it changes from 01 to 00 OR from 00 to 01

if i do it in your example, if i do a restart of HA for example, the automation will always run, because it will have the true or false state, because it will always have some state like unavaliable / 00 / 01

Okay,

than use a Template sensor to extract the required value and expose it as state.

sensor:
  - platform: template
    sensors:
      my_template_sensor:
        value_template: "{{ states.sensor.program.state[02:04] }}"

Next, use this sensor with a State automation trigger.

trigger:
  - platform: state
    entity_id: sensor.my_template_sensor
    from: '01'
    to: '00'
  - platform: state
    entity_id: sensor.my_template_sensor
    from: '00'
    to: '01'
condition: []
action:
  - service: switch.turn_{{ 'on' if trigger.to_state.state == '01' else 'off' }}
    entity_id: switch.xx

yes, that would indeed work, but is it not possible without creating an extra template sensor? because i already already have the text there,there must be a way to create an automation without the extra sensor :slight_smile: