IF/Else with 2 entities double-firing the scene.create snapshot

Hi everyone. This is probably simple, but I can’t wrap my head around a solution.
My goal is straightforward: I want to turn on my led strips red when I start a meeting (mic or webcam switching on) and get those to return to previous state when the meeting is finished (webcam and mic switching off).

The issue I have is that, if I understood correctly, as I add both entities (mic and camera) in the automation, when an app that uses both entities starts, the snapshot scene triggers twice: the mic turns on so it turns the light on and RED, but then the camera also turns on so the automation snapshots the state of the strips as RED once again instead of keeping the old OFF value, overriding the previous snapshot.

What am I doing wrong ? Any idea how to fix it, maybe with some clever conditions?

alias: Meeting Red Light
description: Test for a light turning red when in a meeting.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.battlestation_battlestation_microphoneactive
      - binary_sensor.battlestation_battlestation_webcamactive
    from: "off"
    to: "on"
    id: Mic/Cam On
  - platform: state
    entity_id:
      - binary_sensor.battlestation_battlestation_microphoneactive
      - binary_sensor.battlestation_battlestation_webcamactive
    from: "on"
    to: "off"
condition:
  - condition: time
    after: "08:00:00"
    before: "23:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
action:
  - if:
      - condition: trigger
        id:
          - Mic/Cam On
    then:
      - service: scene.create
        metadata: {}
        data:
          scene_id: before
          snapshot_entities:
            - light.couch_neon_strip_local
            - light.led_strip_office
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 255
            - 0
            - 0
        target:
          entity_id:
            - light.couch_neon_strip_local
            - light.led_strip_office
    else:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.before
mode: single

Split the automation into two. For the “on” case have a condition to check that the webcam or the mic is off. That way you do the snapshot when the first of the two turns on.

you need to add some extra conditions to detect the duplicate. I would say both need to be off for the off action, and only (exactly) one needs to be on for the on action. I think that would give the best results. Requiring both on and both off together (both entities have equal states) is the easiest to implement, but it depends if that is what you want.

The easiest way to accomplisch either variant by the way is putting both trigger entities in a group, and trigger on the state of the group. You can set which behavior you want by setting when the group is on: when all are on or either one is on. Then you do not even need the extra condition, because there’s only one trigger.

2 Likes

MaximTimeClock@bchevreau ,
Create Two Automations One automation with the microphone sensor as the trigger and the other with the webcam sensor as the trigger. Both can have the same actions (scene creation and light turn-on) but without the else block.
This approach separates the logic for each sensor, preventing double execution.
Keep the condition section with the time restriction to ensure the automation only runs during your specified working hours.
You can adjust the scene_id and snapshot_entities based on your actual scene configuration.

Use single as the automation type to block concurrent executions (add a delay: ‘00:00:01’ if the automation doesnt run long),

mode: single

and also add this:

    - condition: template
      value_template: "{{ 300 < ((as_timestamp(now(),'') | float(default=0)) - (as_timestamp(state_attr('automation.xxx','last_triggered'),'') | float(default=0) )) < 31536000 }}"

It will block the automation running if the last time it ran was less than x seconds. Add your automation name in there, and adjust the min/max values as desired.

Alright, I combined a few of your suggestions and ideas and got it to work. I split the automation into 3 IF statements with condition that the other is not on (so that it won’t trigger if I first start my mic and open the cam afterwards) for the first 2, and a 3rd that fires when everything is deactivated.

I also added a 30s delay on the other 2 (that was my first try, it’s likely not needed anymore with conditions?) just to prevent rapid firing in case there’s a bit of delay in reporting with MQTT/HASS.agent and published states get mixed up.

There might be a bit of issues still (haven’t fully tested weird cases yet) and triple IF statement might be overkill, but it seems to work so far.

alias: Meeting Red Light
description: Test for a light turning red when in a meeting.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.battlestation_battlestation_microphoneactive
    from: "off"
    to: "on"
    id: Mic ON
  - platform: state
    entity_id:
      - binary_sensor.battlestation_battlestation_webcamactive
    from: "off"
    to: "on"
    id: Webcam ON
  - platform: state
    entity_id:
      - binary_sensor.battlestation_battlestation_microphoneactive
      - binary_sensor.battlestation_battlestation_webcamactive
    from: "on"
    to: "off"
    id: Mic/webcam OFF
condition:
  - condition: time
    after: "08:00:00"
    before: "23:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
action:
  - if:
      - condition: trigger
        id:
          - Mic ON
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.battlestation_battlestation_webcamactive
            state: "off"
    then:
      - service: scene.create
        metadata: {}
        data:
          scene_id: before
          snapshot_entities:
            - light.couch_neon_strip_local
            - light.led_strip_office
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 255
            - 0
            - 0
        target:
          entity_id:
            - light.couch_neon_strip_local
            - light.led_strip_office
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
  - if:
      - condition: trigger
        id:
          - Webcam ON
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.battlestation_battlestation_microphoneactive
            state: "off"
    then:
      - service: scene.create
        metadata: {}
        data:
          scene_id: before
          snapshot_entities:
            - light.couch_neon_strip_local
            - light.led_strip_office
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 255
            - 0
            - 0
        target:
          entity_id:
            - light.couch_neon_strip_local
            - light.led_strip_office
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
  - if:
      - condition: trigger
        id:
          - Mic/webcam OFF
    then:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.before
mode: single

nuked original.

missed @edwin_d’s second paragraph. single group is definitely the better answer.

1 Like

Just follow @Edwin_D’s advice and put both binary sensors in a group, then trigger off the state of the group. No Ifs or multiple automations required, and you can go back to using your original automation, using only a single trigger instead.

1 Like

T.b.h. I typed the first bit, pressed send, read it again and thought: there’s an easier way. So you probably read it in the time I needed to type part 2. I should have entered “edit:”.

Make a binary sensor helper group that groups your binary sensors into 1 entity. Then use that as the trigger.

alias: Meeting Red Light
description: Test for a light turning red when in a meeting.
trigger:
  - platform: state
    entity_id: binary_sensor.battlestation_mic_and_webcam
    from: "off"
    to: "on"
    id: Mic/Cam On
  - platform: state
    entity_id: binary_sensor.battlestation_mic_and_webcam
    from: "on"
    to: "off"
condition:
  - condition: time
    after: "08:00:00"
    before: "23:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
action:
  - if:
      - condition: trigger
        id:
          - Mic/Cam On
    then:
      - service: scene.create
        metadata: {}
        data:
          scene_id: before
          snapshot_entities:
            - light.couch_neon_strip_local
            - light.led_strip_office
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 255
            - 0
            - 0
        target:
          entity_id:
            - light.couch_neon_strip_local
            - light.led_strip_office
    else:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.before
mode: single