Switch off/on heating when window is open/closed

Hi

I’ve a sensor who detect if the window is opened, and in that case will ask a Shelly relay to switch off heating, then switch on when window is closed.

I’ve done two automations, I’m interested to know if that can be achieved with only one ?

Automations

  trigger:
  - entity_id: binary_sensor.door_window_sensor_1
    for: '10'
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - device_id: 257e0468e7004d109ae999e65d60b
    domain: switch
    entity_id: switch.shellyPM
    type: turn_off

And

  - entity_id: binary_sensor.door_window_sensor_1
    for: '10'
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - device_id: 257e0468e7004d109ae999e65d60b
    domain: switch
    entity_id: switch.shellyPM
    type: turn_on

Thanks

Use the trigger to/from state to derive the service to call.

  trigger:
  - platform: state
    entity_id: binary_sensor.door_window_sensor_1
    # When this changes state and remains stable for 10 seconds...
    for: '00:00:10'
  condition: []
  action:
  # to_state will be either off or on. Do the inverse. 
  - service_template: "switch.turn_{{'off' if trigger.to_state.state == 'on' else 'on'}}"
    entity_id: switch.shellyPM
1 Like

Thanks @jocnnor you’ve helped me to understand templating

I still have one issue, validation force me to add from/to so I had to setup

    to: 'off'

and a second trigger

    to: 'on'

Is there a way to say the trigger is a change whaterver is the to: value ?

I just guessed about being able to use ‘for’ without a ‘from’ or ‘to’. Guess not.

So we’ll just add both as triggers to the same automation. It will work this way as well.

  trigger:
  - platform: state
    entity_id: binary_sensor.door_window_sensor_1
    from: 'off'
    to: 'on'
    for: '00:00:10'
  - platform: state
    entity_id: binary_sensor.door_window_sensor_1
    from: 'on'
    to: 'off'
    for: '00:00:10'
  condition: []
  action:
  # to_state will be either off or on. Do the inverse. 
  - service_template: "switch.turn_{{'off' if trigger.to_state.state == 'on' else 'on'}}"
    entity_id: switch.shellyPM
1 Like

Great
that solution authorize to have different timers

PS: I was looking for something like

    to: ?

But not supported

Just don’t specify ‘to’ and/or ‘from’.

For example:

trigger:
  platform: state
  entity_id: device_tracker.example1
  from: 'home'

This will trigger when the state goes from home to anything else. It’s kind of like saying “to: ?”.

Likewise, you can say

to: 'home'

to trigger when the state goes from anything to ‘home’

Hi Jim

Unfortunately to: is required when using trigger.to_state.state

Ah, ok. If you use my 2nd one above it has 2 triggers with the ‘to’ and ‘from’ posted. Didn’t know to_state required it!

Yep I’ve marked the post to be the solution
thanks

1 Like

Hello, I would like to add a simple automation. If window is open -> heating “off”. If window is closed -> heating “auto”. Can I implement all in one automation, like: If window is open -> heating “off” else heating “auto”?