Binary sensor on turns switch on automation not running

Feeling awkward about posting this as it seems to be such a simple problem but I have tried everything that I can think of to fix the issue including an exhaustive online search.

Background: I set up HA about a month ago for the first time ever since Insteon died. I’ve managed to integrate all of my sonoff, insteon, wyze, wemo, tplink, sengled, unifi, home alarm, tuya local and orbit devices as well as setting up remote access and voice control through Alexa (google integration remains an issue but I’ll save that for a different post!) I’ve scoured all sorts of videos, read blogs and anything else I can find. In the process, I’ve learned about flashing my Wyze camera firmware to RTSP and so forth. I’ve also set up many automations to replace the routines in Alexa as they tend to trigger faster and it removes information from Amazon.

Problem: I am having difficulties with simple IF binary_sensor_xxx turns on THEN turn light.xxx or switch.xxx on. I’ve debugged the automation but it just won’t trigger.

Here is a sample of the yaml code:

alias: ‘Automation_door_opened: Garage door triggers garage lights’
description: ‘’
trigger:

  • platform: state
    entity_id:
    • binary_sensor.garage_door
      to: ‘On’
      from: ‘Off’
      condition: []
      action:
  • service: switch.turn_on
    data: {}
    target:
    entity_id: switch.switchlinc_relay_garage
  • delay:
    hours: 0
    minutes: 5
    seconds: 0
    milliseconds: 0
  • service: switch.turn_off
    data: {}
    target:
    entity_id: switch.switchlinc_relay_garage
    mode: single

Things that I’ve tried:

  • I’ve tried playing around with the triggers so that it just lists To: ‘On’ or just lists From: ‘Off’ even though I don’t think this should matter
  • I’ve tried using the device action instead of call service even though I think the call service is the standard protocol
  • I’ve tried changing the automation mode to “parallel” instead of a single action

Other information:

  • I can confirm that the state of the binary_sensor does change to on in the developer tools
  • I have a separate automation that sends me a notification whenever the binary_sensor is triggered and this automation works perfectly every time
  • this door trigger issue applies to other automations that I’ve tried to implement so clearly there is a problem with the syntax that I’m using

I’m totally stumped and would appreciate any feedback. Thanks in advance.

First please enclose yaml in triple back ticks so it doesn’t mess up the formatting. See here if you don’t know what I mean.

That being said despite the formatting I think I can spot the issue. In your trigger you have 'On' and 'Off'. This is incorrect for a binary sensor, it should be 'on' and 'off'. States are case sensitive.

testing to see if the conventional format appears correctly:

alias: 'Automation_door_opened: Garage door triggers garage lights'
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage_door
    to: 'on'
    from: 'off'
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.switchlinc_relay_garage
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.switchlinc_relay_garage
mode: single

Thanks, Mike for your suggestion. That was totally it and so overwhelmingly simple. Astounds me that one single character makes all the difference! I was so close to throwing in the towel and disconnecting HA out of frustration.

One more question - often, I have two separate automations: one for turning on a light when a door opens and another for turning off the light when the door closes. Is there a way to combine the two with a IF … ELSE type command?

Thanks, again!

Sure. There’s an if-then action which also supports an else. Or there’s a choose action if you need one or more elifs before the else.

For the conditions you can either use a trigger condition and add IDs to each of your triggers after merging them into one automation. Or use a template type condition and look at the trigger object created from your state trigger to decide what action to take.

First experiment with if-then action flops.

alias: 'Notification_app: Back door'
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.back_door
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.back_door
        state: 'on'
    then:
      - service: notify.notify
        data:
          message: Back door opened.
    else:
      - service: notify.notify
        data:
          message: Back door closed.
mode: single

What this automation is meant to do is to say that when a door opens, send a notification indicating that the door is opened and when the door closes, send a notification indicating that the door is closed.

The automation was combined from two separate open/close automations. Whereas the door open part works well, I’m getting multiple notifications telling me that the door is closed which makes sense because of the else part. Is there a way to either refine this automation or to only have the closed notification sent once?

Add this to the trigger:

to:
  - 'on'
  - 'off`

This does two things:

  1. if the binary sensor goes to state unknown or unavailable the trigger won’t fire, currently that will trigger your else
  2. if an attribute changes on the sensor but the state does not the trigger won’t fire, currently that will trigger your else

That should make it so your notifications go from open to closed and you never get a duplicate I think. If not there’s other options but you’ll have to share why it’s happening. To see why open traces and look at the trigger variable by clicking on the first step and going to the changed variables. This shows you what triggered your automation, using this info unwanted triggers can be filtered.

That seems to have done the trick, Mike. Thanks again. Always love learning something from those with more experience.

1 Like