Have a slightly broken automation that includes Xiaomi button and motion detector and many choices

So to get a couple of things out of the way…
HA version - 2024.5.3
Supervisor - 2024.05.1
Running bare metal install on Dell i5 micro system.

I have a fairly complex automation that controls lighting in our bathroom during various intervals of day and night and involves a motion detector. We have dogs and at night it is really dark getting from the dog crates to the front door, so I have deployed a Xiaomi button that I wish to override the motion detector with to activate many of the same lights plus a couple more. I would like to click the button to turn on the lights and then click again to turn them back off.

The problem is that if I get in range of the motion sensor it fires a motion event and due to the nature of the the Xiaomi button retaining the last click type state an event to shut off lights is triggered and should not be. I can see 2 possible ways to make it work. 1) not allow the motion sensor to trigger some how during the late night hours and only run with the button as a trigger. I am really not sure how to accomplish this option. 2) within the subsection of my automatin that takes the click input to turn on the lights have a sequence call that changes the “last click” event to something other than “single”, another thing that I am not sure how to do. I have figured out if I single click and the follow up with a double click that triggers nothing that the motion sensor trigger then does not cause the failure state, so I think option 2 may be the way to go.

I am including the whole automation and then will break out the relevant sections that are causing the problem. Fair warning… I am not the best automation writer and my format may be not the most efficient.

I have not imported this automation into the automation editor as it appears that comments contained within the automation are lost and I do not appear to be able to put them back in and have them visible? Is this something I am doing wrong?

Full automation:

- id: Bathroom_motion
  alias: Bathroom motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001644ca3
      from:
        - 'off'
      to:  
        - 'on'
    - platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.switch_158d0001e5f29e
        click_type: "single"
    - platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.switch_158d0001e5f29e
        click_type: "hold"
  mode: queued
  max: 20
  action:
    - choose:
        - conditions:
    ##########  Motion to turn on lights during daytime  ##########
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: time
              after: '06:00:00'
              before: '20:30:00'
            - condition: state
              entity_id: timer.bathroom_motion_short
              state: 'idle'
            - condition: state
              entity_id: timer.bathroom_motion_long
              state: 'idle'
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: "off"
          sequence:
            - service: light.turn_on
              entity_id:  group.bathroom_lights
              data:
                brightness: 254
                kelvin: 5000
            - service: timer.start
              entity_id: timer.bathroom_motion_long
        - conditions:
    ##########  Motion to turn on lights early night 4000k  ##########
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: time
              after: '20:30:00'
              before: '23:00:00'
            - condition: state
              entity_id: timer.bathroom_motion_short
              state: 'idle'
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: "off"
            - condition: state
              entity_id: input_boolean.night_dog
              state: "off"
          sequence:
            - service: light.turn_on
              entity_id:  group.bathroom_lights
              data:
                brightness: 254
                kelvin: 4000
            - service: timer.start
              entity_id: timer.bathroom_motion_long
        - conditions:
    ##########  Motion to turn on lights night 3750k  ##########
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: state
              entity_id:  timer.bathroom_heat_tag
              state: 'idle'
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: 'off'
            - condition: time
              after: '23:00:00'
              before: '06:00:00'
            - condition: state
              entity_id: input_boolean.night_dog
              state: "off"
          sequence:
            - service: light.turn_on
              entity_id:  light.bathroom_1,light.toolbox,light.sink,light.tree_top
              data:
                brightness: 15
                kelvin: 3750
            - service: light.turn_on
              entity_id: light.computer_lamp
              data:
                brightness: 10
                kelvin: 3750
            - service: timer.start
              entity_id: timer.bathroom_motion_short
    ##########  1 click to turn off lights at night  ##########
        - conditions:
#            - condition: state
#              entity_id: timer.bathroom_motion_short
#              state: 'idle'
#            - condition: state
#              entity_id:  timer.bathroom_motion_long
#              state: 'active'
            - condition: state
              entity_id: binary_sensor.switch_158d0001e5f29e
              attribute: last_action
              state: single 
            - condition: time
              after: '23:00:00'
              before: '06:00:00'
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: "off"
            - condition: state
              entity_id: input_boolean.night_dog
              state: "on"
          sequence:
            - service: timer.finish
              entity_id: timer.bathroom_motion_long
            - service: timer.finish
              entity_id: timer.bathroom_motion_short
            - service: input_boolean.turn_off
              entity_id: input_boolean.night_dog
            - service: light.turn_off
              entity_id: light.door,light.computer_lamp,light.tree_top,light.sink,light.toolbox
    ##########  1 click to turn on lights at night  ##########
        - conditions:
#            - condition: state
#              entity_id: timer.bathroom_motion_short
#              state: 'idle'
#            - condition: state
#              entity_id:  timer.bathroom_motion_long
#              state: 'idle'  
            - condition: time
              after: '23:00:00'
              before: '06:00:00'
            - condition: state
              entity_id: binary_sensor.switch_158d0001e5f29e
              attribute: last_action
              state: single
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: "off"
            - condition: state
              entity_id: input_boolean.night_dog
              state: "off"
          sequence:
            - service: timer.finish
              entity_id: timer.bathroom_motion_short
            - service: timer.finish
              entity_id: timer.bathroom_motion_long
            - service: input_boolean.turn_on
              entity_id: input_boolean.night_dog
            - service: light.turn_on
              entity_id: light.bathroom_1,light.toolbox,light.sink,light.tree_top,light.computer_lamp
              data:
                brightness: 15
                kelvin: 3000
#            - service: timer.start
#              entity_id: timer.bathroom_motion_long
            - service: light.turn_on
              entity_id: light.door
              data:
                brightness: 254
              
    ##########  Hold to toggle lights at night  ##########
#        - conditions:
#            - condition: state
#              entity_id: binary_sensor.switch_158d0001e5f29e
#              attribute: last_action
#              state: hold
#            - condition: time
#              after: '22:30:00'
#              before: '06:00:00'
#          sequence:
#            - service: light.toggle
#              entity_id: light.door
###################################################

- id: Bathroom_long_timer_expired
  alias: Bathroom long timer expired
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.bathroom_motion_long
  condition: 
    condition: template
    value_template: >
      {{ not is_state('timer.bathroom_heat_tag','active') }} 
  action:
  - service: light.turn_off
    entity_id: group.bathroom_lights,light.door
    
- id: Bathroom_short_timer_expired
  alias: Bathroom short timer expired
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.bathroom_motion_short
  action:
    - choose:
        - conditions:
            - condition: time
              after: '06:00:00'
              before: '23:00:00'
            - condition: template
              value_template: >
                {{ not is_state('timer.bathroom_heat_tag','active') }}
          sequence:
            - service: light.turn_off
              entity_id: group.bathroom_lights
        - conditions:
            - condition: time
              after: '23:00:00'
              before: '06:00:00'
            - condition: template
              value_template: >
                {{ not is_state('timer.bathroom_heat_tag','active') }}
          sequence:
            - service: light.turn_off
              entity_id: group.bathroom_lights,light.tree_top,light.sink,light.computer_lamp

Sections that are causing the problem:
Lights turning on with click:

    ##########  1 click to turn on lights at night  ##########
        - conditions:
#            - condition: state
#              entity_id: timer.bathroom_motion_short
#              state: 'idle'
#            - condition: state
#              entity_id:  timer.bathroom_motion_long
#              state: 'idle'  
            - condition: time
              after: '23:00:00'
              before: '06:00:00'
            - condition: state
              entity_id: binary_sensor.switch_158d0001e5f29e
              attribute: last_action
              state: single
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: "off"
            - condition: state
              entity_id: input_boolean.night_dog
              state: "off"
          sequence:
            - service: timer.finish
              entity_id: timer.bathroom_motion_short
            - service: timer.finish
              entity_id: timer.bathroom_motion_long
            - service: input_boolean.turn_on
              entity_id: input_boolean.night_dog
            - service: light.turn_on
              entity_id: light.bathroom_1,light.toolbox,light.sink,light.tree_top,light.computer_lamp
              data:
                brightness: 15
                kelvin: 3000
#            - service: timer.start
#              entity_id: timer.bathroom_motion_long
            - service: light.turn_on
              entity_id: light.door
              data:
                brightness: 254

Lights turning off with click and being screwed up by the “last action” state of the button and motion activation as a main trigger.

    ##########  1 click to turn off lights at night  ##########
        - conditions:
#            - condition: state
#              entity_id: timer.bathroom_motion_short
#              state: 'idle'
#            - condition: state
#              entity_id:  timer.bathroom_motion_long
#              state: 'active'
            - condition: state
              entity_id: binary_sensor.switch_158d0001e5f29e
              attribute: last_action
              state: single 
            - condition: time
              after: '23:00:00'
              before: '06:00:00'
            - condition: state
              entity_id: input_boolean.notify_bathroom_tag
              state: "off"
            - condition: state
              entity_id: input_boolean.night_dog
              state: "on"
          sequence:
            - service: timer.finish
              entity_id: timer.bathroom_motion_long
            - service: timer.finish
              entity_id: timer.bathroom_motion_short
            - service: input_boolean.turn_off
              entity_id: input_boolean.night_dog
            - service: light.turn_off
              entity_id: light.door,light.computer_lamp,light.tree_top,light.sink,light.toolbox

The timer finish in the above kills the lights in the bathroom that may have been triggered by the motion sensor and then the lights that are triggered by the button press are turned off as well.

I am really hoping that someone will see something that I have overlooked… I have been beating my head for way to long to attempt to figure this little but out in this automation. I have left test code in place commented out as well.

Thanks Matt

Are you trying to do too many things in one automation? :grin:

Multiple triggers are fine when they are connected in some way, but the motion sensor and the button are different animals. You can use input_booleans as flags if you want to pass conditions from one automation to the other.

Even if I broke it all apart, would not I still have the same problem of cross influences?

So I just broke out all of the button actions to another automation stack and left the boolean trigger in the timer finish routine. I will test again tonight and see if it works better. I also did a little clean up that may help. If it works, I will post the resulting automations for benefit.

1 Like