Linking physical switch to smart bulb with conditions

I have a zigbee 3 gang with a left over switch I’ve linked to a WiFi bulb. Base automation on & off via app or physical touch works fine. My issue starts where conditions for how the lamp should turn on aren’t working.

When turned on by the switch, it completely disregards any of the 4 actions laid out. Using I f-then conditions for a singular action work fine but I’ll not be able to stack the other 3 action choices if I do this.

Just had a thought as I typed this; should I keep the switch link confined to on-off & apply conditions to the bulb itself in a separate automation?

alias: Switch link (Main Hall Lamp)
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.main_hall_outside_lights_back_and_side_l2
    from: "on"
    to: "off"
    id: MainL2OnToOff
  - platform: state
    entity_id:
      - switch.main_hall_outside_lights_back_and_side_l2
    from: "off"
    to: "on"
    id: MainL2OffToOn
  - platform: state
    entity_id:
      - light.main_hall_lamp
    from: "on"
    to: "off"
    id: MainHallLampOff
  - platform: state
    entity_id:
      - light.main_hall_lamp
    from: "off"
    to: "on"
    id: MainHallLampOn
condition: []
action:
  - parallel:
      - choose:
          - conditions:
              - condition: trigger
                id:
                  - MainL2OnToOff
            sequence:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.main_hall_lamp
          - conditions:
              - condition: trigger
                id:
                  - MainL2OffToOn
            sequence:
              - choose:
                  - conditions:
                      - condition: sun
                        before: sunset
                        after: sunrise
                    sequence:
                      - service: light.turn_on
                        data:
                          brightness_pct: 90
                          kelvin: 6500
                        target:
                          entity_id: light.main_hall_lamp
                  - conditions:
                      - condition: numeric_state
                        entity_id: sensor.mmwave_sensor_1_illuminance_lux
                        below: 50
                        enabled: false
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: media_player.main_hall_cast
                            state: playing
                          - condition: state
                            entity_id: media_player.playstation_4
                            state: "on"
                          - condition: state
                            entity_id: media_player.main_hall_cast
                            state: buffering
                          - condition: state
                            entity_id: media_player.main_hall_cast
                            state: paused
                          - condition: state
                            entity_id: media_player.playstation_4
                            state: playing
                          - condition: state
                            entity_id: media_player.playstation_4
                            state: paused
                    sequence:
                      - service: light.turn_on
                        data:
                          brightness_pct: 10
                          kelvin: 2446
                        target:
                          entity_id: light.main_hall_lamp
                  - conditions:
                      - condition: and
                        conditions:
                          - condition: sun
                            after: sunset
                          - condition: time
                            before: "20:30:00"
                    sequence:
                      - service: light.turn_on
                        data:
                          brightness_pct: 90
                          kelvin: 2770
                        target:
                          entity_id: light.main_hall_lamp
                  - conditions:
                      - condition: sun
                        before: sunrise
                        after: sunset
                        enabled: false
                      - condition: time
                        after: "20:30:00"
                        before: "07:00:00"
                    sequence:
                      - service: light.turn_on
                        data:
                          brightness_pct: 25
                          kelvin: 2345
                        target:
                          entity_id: light.main_hall_lamp
      - choose:
          - conditions:
              - condition: trigger
                id:
                  - MainHallLampOff
            sequence:
              - service: switch.turn_off
                data: {}
                target:
                  entity_id:
                    - switch.main_hall_outside_lights_back_and_side_l2
          - conditions:
              - condition: trigger
                id:
                  - MainHallLampOn
            sequence:
              - service: switch.turn_on
                data: {}
                target:
                  entity_id:
                    - switch.main_hall_outside_lights_back_and_side_l2
mode: single

could you check the code here and repost it so that it’s readable?

Updated. Never posting code through mobile again.

2 Likes

i must admit, i don’t really understand it all. so if someone else here has their head wrapped around this and spots the issue or if you don’t want to follow my approach to this, then ignore me…

my first reaction is that the code seems a bit complicated. and i tend to believe that the more complicated code is, the more fragile it is.

if i understand the overall code, i think you have 4 automations lumped into 1, and the 4 different automations don’t actually leverage each other (ie, there’s no benefit to have them together).

you have 2 major blocks running in parallel to each other (main hall lights back & side 12 and main hall lamp). not sure why they are parallel. the triggers either pertain to 1 or the other and not both.

if i reduce it down, i think you’re saying there’s something wrong in this simplified version? is that right?

alias: Switch link (Main Hall Lamp) - on
trigger:
  - platform: state
    entity_id:
      - switch.main_hall_outside_lights_back_and_side_l2
    to: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: sun
            before: sunset
            after: sunrise
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 90
              kelvin: 6500
            target:
              entity_id: light.main_hall_lamp
      - conditions:
          - condition: numeric_state
            entity_id: sensor.mmwave_sensor_1_illuminance_lux
            below: 50
            enabled: false
          - condition: or
            conditions:
              - condition: state
                entity_id: media_player.main_hall_cast
                state: playing
              - condition: state
                entity_id: media_player.playstation_4
                state: "on"
              - condition: state
                entity_id: media_player.main_hall_cast
                state: buffering
              - condition: state
                entity_id: media_player.main_hall_cast
                state: paused
              - condition: state
                entity_id: media_player.playstation_4
                state: playing
              - condition: state
                entity_id: media_player.playstation_4
                state: paused
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 10
              kelvin: 2446
            target:
              entity_id: light.main_hall_lamp
      - conditions:
          - condition: and
            conditions:
              - condition: sun
                after: sunset
              - condition: time
                before: "20:30:00"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 90
              kelvin: 2770
            target:
              entity_id: light.main_hall_lamp
      - conditions:
          - condition: sun
            before: sunrise
            after: sunset
            enabled: false
          - condition: time
            after: "20:30:00"
            before: "07:00:00"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 25
              kelvin: 2345
            target:
              entity_id: light.main_hall_lamp

if this is correct, could you

  1. clarify this:
    When turned on by the switch, it completely disregards any of the 4 actions laid out. Using I f-then conditions for a singular action work fine but I’ll not be able to stack the other 3 action choices if I do this.

what do you mean by ‘the switch’? do you mean switch.main_hall_outside_lights_back_and_side_12? or do you mean the “left over switch”? i’m not sure how that leftover switch comes into play is that a dumb switch? is that switch cutting off power to the wifi bulb?

if the issue is in this block, could you try removing this from your automation and adding mine as a second standalone automation, then run it and post the trace. tell us what actually happened and what should have happened instead.

When the automation is triggered, it doesn’t choose any of the 4 action options. Regardless of the state of the devices in option 2, time or other conditions, it just defaults to a plain white.

The switch is a 3 gang Zigbee switch that replaces a 2 gang dumb switch.

switch.main_hall_outside_lights_back_and_side_L1
switch.main_hall_outside_lights_back_and_side_L2
switch.main_hall_outside_lights_back_and_side_L3

The result is I have 1 rockers left over I can assign to entities or automations. For this, I’ve assigned L2 to handle the bulb. L1 handles the main lights & L3 handles an outdoor light as they still use the original electric connections. L2 works as a switch but since it doesn’t have a physical connection to anything, I’m using this automation to connect it to the wifi bulb. In the initial design it worked fine. I’m now trying to add in various conditions that the bulb will reflect: TV on, PS on, early evening, late night, etc. It’s running 2 options in parallel so that the state of the bulb & switch always match like a normal light switch.

2 tries of your code worked. I’ll try the other time conditions in the morning:
None of the devices in Option 2 were in the required states.

Triggered by the state of switch.main_hall_outside_lights_back_and_side_l2 at April 7, 2024 at 22:51:20
Choose: Option 4 executed
(light.main_hall_lamp) turned on
Finished at April 7, 2024 at 22:51:20 (runtime: 0.16 seconds)

Had one of the devices in Option 2 in the required state and it chose this over Option 4

Triggered by the state of switch.main_hall_outside_lights_back_and_side_l2 at April 7, 2024 at 22:58:05
Choose: Option 2 executed
(light.main_hall_lamp) turned on
Finished at April 7, 2024 at 22:58:06 (runtime: 0.15 seconds)

in the first trace you posted, the trigger looks like it was MainHallLampOn

as such, none of the 4 conditions in the first block should trigger right? only the second condition in the second block.

That’s what’s currently happening. If anything I expected the exact opposite: 1 of the 4 conditions in the first block to be triggered and 1 of 2 in the second block.

why is that? in the trace trigger id MainHallLampOn happened. there is only one condition in the second block that your code has been instructed to run when MainHallLampOn is the id.

which other code block condition did you expect to run on this trigger id?


In the first block, one parallel automation matches the switch to the bulb.

In the second, it matches the bulb to the switch but with conditions on how the bulb should behave. I’m on the verge of splitting the automation in two:
1st dedicated to matching the bulb & switch
2nd dedicated to applying the conditions to the bulb