Sensor as virtual switch?

Use case: Basement utility closet with a Z-wave door/window sensor and a Z-wave light switch. When the door sensor is open, I want the light to turn on. When closed, light turned off.

Is there an existing integration where I can combine these two? Or do I need two automations (or one automation that just sets the sate of the light to the state of the sensor)?

Thanks

You can make an automation with 2 triggers and a choose action something like this:

alias: My Automation
description: ''
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.your_door_contact
    from: 'off'
    to: 'on'
  - platform: state
    entity_id:
      - binary_sensor.your_door_contact
    from: 'on'
    to: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.your_door_contact
            state: 'on'
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.your_light
      - conditions:
          - condition: state
            entity_id: binary_sensor.your_door_contact
            state: 'off'
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.your_light
    default: []
1 Like

One automation should be enought by replicating the state of the door to the light…

trigger:
   - platform: state
     entity_id: binary_sensor.your_z_wave_door
     to:
action:
   - service: "light.turn_{{states('binary_sensor.your_z_wave_door')}}"
     target:
       entity_id: light.your_z_wave_light
1 Like

Ok so this works as expected. When I open the door, the light comes on. When I close the door, the light goes off.

alias: Living Room light test with monoprice sensor
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.monoprice_doorsensor_access_control_window_door_is_open
condition: []
action:
  - service: >-
      light.turn_{{states('binary_sensor.monoprice_doorsensor_access_control_window_door_is_open')}}
    target:
      entity_id: light.living_room_fan
mode: single

However these are closet lights and sometimes the door is left open so I want to turn the light off after 20 minutes.

This turns the light on when I open the door, but does not turn it off when I close the door. The light does turn off after 20 minutes.

alias: Theater Closet Light on/off from sensor
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.basement_closet_sensor_any
condition: []
action:
  - service: switch.turn_{{states('binary_sensor.basement_closet_sensor_any')}}
    target:
      entity_id: switch.basement_basement_closet
  - delay:
      hours: 0
      minutes: 20
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.basement_basement_closet
mode: single

Any way to have a single automation like this where the ligt turns off on a timer AND/OR if the automation is re-triggered with a state change, to immediately turn the light off?

Thanks

1 Like

The automation is in mode: single… So when the automation has been triggered any new trigger while running will not do anything (you should have a warning in the logfile)… The mode should be “restart” to stop the current execution and start a new run with the new trigger…

That was it. Thank you very much

This worked great for a while (and still does) but I am getting frequent error alerts in restart like:

" The automation “Cabinet Lights Lutron Keypad Light” (automation.counter_lights_lutron_keypad_light_2 ) has an action that calls an unknown service: switch.turn_unavailable ."

Like it is trying to validate the automation before the switch service reports an on/off status and instead reports unavailable which causes an issue. Any way to suppress these alerts?

That’s probably due to the fact that the entity triggering the automation is unavailable at startup which is a change of state… So in your automation you have to check that the state triggering the automation is ON or OFF all the other value should not start the actions… So I would do something like this…

trigger:
   - platform: state
     entity_id: binary_sensor.your_z_wave_door
     to: 
        - "on"
        - "off"