Automation help with an exception situation

I have created an automation that I need a little push in the correct direction for how to program for an exception situation that occurs.

I have hass.agent sending my desktop microphone process status to HA. I have two lights (a kauf bulb and a wyze bulb) turn red to let others in the home know that I am on the phone. I do this by having the automation perform the following:

I have two trigger states: changing to “Softphone” and changing from “Softphone” - both of them have IDs
The action is a CHOOSE
Option 1:When the status changes to “Softphone”, IF kauf bulb is on for 10 seconds, THEN create a scene of its current state, call service light.turn_on to change the color to darkred. And the same for the wyze bulb.
Option 2: When the status changes from “Softphone”, IF Kauf bulb is on, THEN change SCENE “bulb previous state”. and same for the wyze bulb.

Situation 1
How do I program for the situation that someone turns off the light while it is red? When they turn it back on, it is set to always on previous state in the bulb settings.

Situation 2
How do I program for the situation when someone turns on the light long after I am on the phone and the trigger has run

If I set the automation to repeat, it will overwrite the temporary scene.

alias: On the phone
description: Change lighting modes when on the phone
trigger:
  - platform: state
    entity_id:
      - sensor.m70q_microphoneprocess
    for:
      hours: 0
      minutes: 0
      seconds: 5
    to: Softphone
    id: Softphone_Active
  - platform: state
    entity_id:
      - sensor.m70q_microphoneprocess
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: Softphone_Inctive
    from: Softphone
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Softphone_Active
        sequence:
          - if:
              - condition: state
                entity_id: light.kauf_bulb_4
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 10
            then:
              - service: scene.create
                data:
                  scene_id: basement4_state_before_phone
                  snapshot_entities: light.kauf_bulb_4
              - service: light.turn_on
                data:
                  color_name: darkred
                  transition: 3
                target:
                  device_id: ddea4bfdf5f155910f9d932d4efd5b95
          - if:
              - condition: state
                entity_id: light.dining_table_2
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 10
            then:
              - service: scene.create
                data:
                  scene_id: dining2_state_before_phone
                  snapshot_entities: light.dining_table_2
              - service: light.turn_on
                data:
                  color_name: darkred
                  transition: 3
                target:
                  device_id: 2cc9b5231dfe5d2a614242a97927a3a7
      - conditions:
          - condition: trigger
            id: Softphone_Inctive
        sequence:
          - if:
              - condition: device
                type: is_on
                device_id: ddea4bfdf5f155910f9d932d4efd5b95
                entity_id: light.kauf_bulb_4
                domain: light
                for:
                  hours: 0
                  minutes: 0
                  seconds: 10
            then:
              - service: scene.turn_on
                target:
                  entity_id: scene.basement4_state_before_phone
                metadata: {}
          - if:
              - condition: device
                type: is_on
                device_id: 2cc9b5231dfe5d2a614242a97927a3a7
                entity_id: light.dining_table_2
                domain: light
            then:
              - service: scene.turn_on
                target:
                  entity_id: scene.dining2_state_before_phone
                metadata: {}
mode: single

Hey mate, welcome to the community. Can you please format your code as per here.

Cheers.

Sorry I’m a noob obviously. I made some changes and I kinda got it working. See how awful this looks:

alias: On the phone
description: Change lighting modes when on the phone
trigger:
  - platform: state
    entity_id:
      - sensor.m70q_microphoneprocess
    for:
      hours: 0
      minutes: 0
      seconds: 5
    to: Softphone
    id: Softphone_Active
  - platform: state
    entity_id:
      - sensor.m70q_microphoneprocess
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: Softphone_Inctive
    from: Softphone
  - platform: state
    entity_id:
      - light.kauf_bulb_4
    from: unavailable
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
    id: Basement4 Turned On
  - platform: state
    entity_id:
      - light.kauf_bulb_4
    from: "on"
    to: unavailable
    id: Basement 4 Turned off
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - platform: state
    entity_id:
      - light.dining_table_2
    from: unavailable
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
    id: Dining2 Turned On
  - platform: state
    entity_id:
      - light.dining_table_2
    from: "on"
    to: unavailable
    id: Dining 2 Turned off
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: light.kauf_bulb_4
        state: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 10
      - condition: state
        entity_id: light.dining_table_2
        state: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 10
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Softphone_Active
        sequence:
          - if:
              - condition: state
                entity_id: light.kauf_bulb_4
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            then:
              - service: scene.create
                data:
                  scene_id: basement4_state_before_phone
                  snapshot_entities: light.kauf_bulb_4
              - service: light.turn_on
                data:
                  color_name: darkred
                  transition: 3
                target:
                  device_id: ddea4bfdf5f155910f9d932d4efd5b95
          - if:
              - condition: state
                entity_id: light.dining_table_2
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            then:
              - service: scene.create
                data:
                  scene_id: dining2_state_before_phone
                  snapshot_entities: light.dining_table_2
              - service: light.turn_on
                data:
                  color_name: darkred
                  transition: 3
                target:
                  device_id: 2cc9b5231dfe5d2a614242a97927a3a7
      - conditions:
          - condition: trigger
            id: Softphone_Inctive
        sequence:
          - if:
              - condition: device
                type: is_on
                device_id: ddea4bfdf5f155910f9d932d4efd5b95
                entity_id: light.kauf_bulb_4
                domain: light
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            then:
              - service: scene.turn_on
                target:
                  entity_id: scene.basement4_state_before_phone
                metadata: {}
          - if:
              - condition: device
                type: is_on
                device_id: 2cc9b5231dfe5d2a614242a97927a3a7
                entity_id: light.dining_table_2
                domain: light
            then:
              - service: scene.turn_on
                target:
                  entity_id: scene.dining2_state_before_phone
                metadata: {}
                enabled: false
              - repeat:
                  count: "3"
                  sequence:
                    - service: scene.turn_on
                      target:
                        entity_id: scene.dining2_state_before_phone
                      metadata: {}
      - conditions:
          - condition: trigger
            id: Basement4 Turned On
          - condition: state
            entity_id: sensor.m70q_microphoneprocess
            state: Softphone
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - if:
              - condition: state
                entity_id: light.kauf_bulb_4
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            then:
              - service: scene.create
                data:
                  scene_id: basement4_state_before_phone
                  snapshot_entities: light.kauf_bulb_4
              - service: light.turn_on
                data:
                  color_name: darkred
                  transition: 3
                target:
                  device_id: ddea4bfdf5f155910f9d932d4efd5b95
      - conditions:
          - condition: trigger
            id: Basement 4 Turned off
          - condition: state
            entity_id: sensor.m70q_microphoneprocess
            state: Softphone
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - light.kauf_bulb_4
                from: unavailable
                to: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 10
          - service: scene.turn_on
            target:
              entity_id: scene.basement4_state_before_phone
            metadata: {}
      - conditions:
          - condition: trigger
            id: Dining2 Turned On
          - condition: state
            entity_id: sensor.m70q_microphoneprocess
            state: Softphone
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - if:
              - condition: state
                entity_id: light.dining_table_2
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            then:
              - service: scene.create
                data:
                  scene_id: dining2_state_before_phone
                  snapshot_entities: light.dining_table_2
              - service: light.turn_on
                data:
                  color_name: darkred
                  transition: 3
                target:
                  device_id: 2cc9b5231dfe5d2a614242a97927a3a7
      - conditions:
          - condition: trigger
            id: Dining 2 Turned off
          - condition: state
            entity_id: sensor.m70q_microphoneprocess
            state: Softphone
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - light.dining_table_2
                from: unavailable
                to: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 10
          - delay:
              hours: 0
              minutes: 1
              seconds: 0
              milliseconds: 0
          - repeat:
              count: "3"
              sequence:
                - service: scene.turn_on
                  target:
                    entity_id: scene.dining2_state_before_phone
                  metadata: {}
mode: single

I’m sure this isn’t very efficient code. I added multiple triggers with conditions. I had to add a delay and repeats for the wyze bulbs because they are far from reliable from what I have seen.

Why are you tracking the changes to/from unavailable? Don’t you want to track off instead?

Your conditions prevents the automation to run when both lights are off, is this what you want?

I’m trying to understand your logic…

Why are you repeating this 3 times? It should work with just one time, no?

You would need to trigger on the light becoming available again and then test if you are on the phone or not to determine if the light still needs to be red, or if it should call the scene which gives the previous colour.

The same solution would fix ‘situation 2’.

EDIT: at a glance it looks like this is what you have done.

I did and it works so far.

The Wyze bulbs don’t seem to listen the first time every time.

Changing to off would be one I would use if folks were controlling the lights with home assistant. The other folks in the home are just using the light switch. I could also add that or change my logic to be “from ON”.

This is not expected to any home automation system and you should invest some time to get this working in a reliable way.

Considering moving away from them entirely. So I’ll likely go zigbee or Wi-Fi esphome. We use Sneider electric zigbee devices at work (industrial automation) and they work pretty darn well.

I think what actually got the wyze bulbs working was the one minute delay. It doesn’t look like they like obeying a command from the api as soon as they are powered on. I also experience the same thing with their native app.