Automation -> Action with if/else template

HI
i try to create automation for control switch in depends of chedule state
but not clear why is not work
could help someone where am do mistake ?
thanks

  - id: water_boiler_auto
    alias: water_boiler_auto
    trigger:
     - platform: state
       entity_id: chedule.water_heater
       from: 'on'
       to: 'off'
     - platform: state
       entity_id: chedule.water_heater
       from: 'off'
       to: 'on'
    action:
     - service_template: >
        {% if states('chedule.water_heater') == 'on' %}
         switch.turn_off
        {% else %}
         switch.turn_on
        {% endif %}
       entity_id: switch.water_boiler

You have a couple problems… you’re missing an “s” on “schedule.water_heater”. Using service_template is not necessary, you just use service. You can use the “trigger” variable to access the value your trigger entity has changed from to set the switch service correctly.

  - id: water_boiler_auto
    alias: water_boiler_auto
    trigger:
     - platform: state
       entity_id: schedule.water_heater
       from:
         - 'on'
         - 'off'
       to:
         - 'on'
         - 'off'
    action:
     - service: switch.turn_{{ trigger.from_state.state }}
       entity_id: switch.water_boiler

EDIT: Corrected issue pointed out by pnbruckner

3 Likes

Shouldn’t the service be the other way, based on the OP? E.g.,

switch.turn_{{ 'off' if trigger.to_state.state == 'on' else 'on' }}

Or maybe even:

switch.turn_{{ trigger.from_state.state }}
1 Like

I think, the OP want’s the following:

IF schedule_waterheater is OFF
→ and switch has state == ON
THEN: switch.Turn off

IF schedule_waterheater is ON
→ and switch has state OFF
THEN switch.Turn on

IF schedule_waterheater is OFF
→ and switch has state OFF
THEN do nothing

IF schedule_waterheater is ON
→ and switch has state ON
THEN do nothing

That are the four rules wich I understand from the design I can think of based on the description…
Maybe, just to clarify the exact behave with the TO?

Great! thanks for help , especially for correct my foolish mistakes (missed “s”) its work now !
But hhow to reverce action in depends of trigger value ? when schedule.water_heater =’“on” → switch to “off” ?

trigger → when schedule is change state
action → depends of schedule state if its ''on " → switch go to “off” else → on

It should be working in both directions without any additional changes… when the schedule entity changes from “off” to “on” it will call the switch.turn_off service.

Did you see the edit where {{ trigger.to_state.state }} was changed to {{ trigger.from_state.state }}?

1 Like

yea! now its work in my way
thanks

 - id: water_boiler_auto
   alias: water_boiler_auto
   trigger:
    - platform: state
      entity_id: schedule.water_heater
      from:
        - 'on'
        - 'off'
      to:
        - 'on'
        - 'off'
   action:
    - service: switch.turn_{{ trigger.from_state.state }}
      entity_id: switch.water_boiler

Anyone know under what circumstances a schedule entity’s value can be anything other than on or off?

For example, what could make it unavailable (or unknown)? Perhaps momentarily at startup?

Just wondering if specifying to/from state-changes is necessary (for this application) if the odds are zero of it every having anything other than nominal states values.