Pair of complex automaions not playing well together, could use a little help

I have 2 automations that appear to not play well together. The first uses a motion sensor to turn on and off bathroom lights and the second primarily controls a space heater in the same bathroom. When someone is taking a shower I would like the light to stay on just a little longer than the heater. The heater automation is controlled by an NFC tag. I have that tag enable the heater and the lights for 45 minutes plus a small delay when turning off the heater before the lights turn off and once again become the domain of the motion controller.

What appears to be happening is the the motion sensor turns the lights on when entering, as expected. Then the tag is scanned turning on the heater and the lights for 45 minutes controlled by a delay in that automation. But it appears that the motion sensor delay is shutting the lights off early thus overriding the 45 minute delay in the heater automation.

So, I think that somehow the heater automation needs to interact with the motion sensor automation.

Heater tag scan automation:

- id: nfc bathroom heat 
  alias: NFC bathroom heat
  mode: restart
  trigger:
    - platform: tag
      tag_id: 5c75bdd6-20d2-4d08-a28b-a3944363782c
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: switch.bathroom_heater
              state: 'off'
          sequence:
            - service: switch.turn_on
              entity_id: switch.bathroom_heater
            - service: light.turn_on
              entity_id: light.bathroom_1,light.bathroom_2
            - delay:
                minutes: 45
            - service: switch.turn_off
              entity_id: switch.bathroom_heater
        - conditions:
            - condition: state
              entity_id: switch.bathroom_heater
              state: 'on'
          sequence:
            - service: switch.turn_off
              entity_id: switch.bathroom_heater
            - delay:
                minutes: 5
            - service: light.turn_off
              entity_id: light.bathroom_1,light.bathroom_2  

Motion automation:

- id: Bathroom motion
  alias: Bathroom motion
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001644ca3
    to:
      - 'on'
      - 'off'
  mode: restart
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: time
              after: '08:00:00'
              before: '23:00:00'
          sequence:
            - service: light.turn_on
              entity_id:  light.bathroom_1,light.bathroom_2,light.toolbox
              data:
                brightness: 254
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: time
              after: '23:00:00'
              before: '05:30:00'
          sequence:
            - service: light.turn_on
              entity_id:  light.bathroom_1,light.bathroom_2,light.toolbox,light.sink
              data:
                brightness: 20
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'off' }}"
            - condition: time
              after: '07:45:00'
              before: '05:15:00'
          sequence:
            - delay: "00:15"
            - service: light.turn_off
              entity_id: light.bathroom_1,light.bathroom_2,light.toolbox,light.sink

Would this be as simple as setting the motion sensor to off before triggering the lights? But, if I did that and someone triggered the sensor it would start the whole error over?

I am going to be gone for a few days and may not be able to reply, but will be watching the discussion and will work on it more when I get back.

Hi - This is most likely the motion sensor not seeing anyone for a moment and turning off when you donā€™t expect it to.

I recall reading somewhere (canā€™t find it at the moment) that itā€™s not really a good idea to use the delay feature - a lot can happen in 45 minutes. Some might not agree with that.

Instead of using delay, I start a timer. When the timer finishes, that event triggers an automation that turns off the lights. For example:

- alias: Motion sensor state change actions
  trigger:
  - entity_id:
    - binary_sensor.bathroom_motion_sensor
    from: 'off'
    to: 'on'
    platform: state
  - entity_id:
    - binary_sensor.bathroom_motion_sensor
    from: 'on'
    to: 'off'
    platform: state
  action:
  - service: light.turn_on
    entity_id: light.xyz
  - service: timer.start
    entity_id: timer.bathroom_light_timer

The timer is defined in the configuration file as (set the time to whatever works for you - you can set the time in the automation when you start it):

timer:
  bathroom_light_timer:
    duration: '00:10:00'

Whenever the motion sensor changes state (either on to off or off to on), the lights turn on and the timer starts.

Then when the timer finishes, this automation is triggered:

- alias: Expired timer manager
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.test_timer
  action:
  - service: light.turn_off
    entity_id: light.xyz

Depending on the motion sensor, it likely is only on for a couple seconds unless it is repeatedly triggered, and then turns off. Even a momentary off because it didnā€™t see anyone will cause your lights to turn off and then turn back on once the motion sensor is triggered on again.

It likely isnā€™t triggered when someone is in the shower. It turns off, and then out go the lights. I had this problem in our laundry room (totally irritating my better half more than once), and the logic above fixed that - note that I start the timer when it triggers on and then start it again when the timer triggers off. That way the lights are always on for the duration of the timer after the motion sensor turns off to make sure no one is stranded in the dark - this made the logic much more robust.
2.

You could do a similar thing with the heater logic - just start a second timer once the tag is scanned. You could also re-start the light timer, too, to add robustness. That should keep everything ā€œsyncedā€ up.

Hope this helps! Iā€™ve lost track of how much time Iā€™ve spent trying to get to a robust motion sensor solution!

When I get home, i will try to make sense of thisā€¦ thanks. You stated the delay issue better than I did, that is exactly what appears to be going on.

Hope it works out well! Let me know.

So, the motion sensor triggers the same lights as the tag. But, I want a different time on frame if triggered by the tag. How will this override the motion sensor timer that is triggered by walking by to scan the tag before a shower?

The current problem is that the lights turn off based on the motion sensor delay and no based on the later triggered tag scan. I do want to maintain different on times for the 2 situations.

Here are the automations that I ended up with.

- id: nfc bathroom heat 
  alias: NFC bathroom heat
  mode: restart
  trigger:
    - platform: tag
      tag_id: 5c75bdd6-20d2-4d08-a28b-a3944363782c
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: switch.bathroom_heater
              state: 'off'
          sequence:
            - service: switch.turn_on
              entity_id: switch.bathroom_heater
            - service: light.turn_on
              entity_id: light.bathroom_1,light.bathroom_2
            - service: timer.start
              entity_id: timer.bathroom_heat_tag
            - service: timer.cancel
              entity_id: timer.bathroom_motion_long
            - service: timer.cancel
              entity_id: timer.bathroom_motion_short
        - conditions:
            - condition: state
              entity_id: switch.bathroom_heater
              state: 'on'
          sequence:
            - service: timer.finish
              entity_id: timer.heater
#            - delay:
#                minutes: 5
            - service: timer.start
              entity_id: timer.bathroom_motion_short
            - service: light.turn_on
              entity_id: light.bathroom_1,light.bathroom_2

- id: Bathroom heat timer expired
  alias: Bathroom heat timer expired
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.bathroom_heat_tag
  action:
  - service: switch.turn_off
    entity_id: switch.bathroom_heater
  - service: timer.start
    entity_id: timer.bathroom_motion_short

    
- id: Bathroom heat timer expired light delay
  alias: Bathroom heat timer expired light delay
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.bathroom_motion_short
  action:
  - service: light.turn_off
    entity_id: light.bathroom_1,light.bathroom_2
- id: Bathroom motion
  alias: Bathroom motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001644ca3
      from:
        - 'on'
      to:  
        - 'off'
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001644ca3
      from:
        - 'off'
      to:  
        - 'on'
  mode: restart
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: time
              after: '08:00:00'
              before: '23:00:00'
          sequence:
            - service: light.turn_on
              entity_id:  light.bathroom_1,light.bathroom_2,light.toolbox
              data:
                brightness: 254
            - service: timer.start
              entity_id: timer.bathroom_motion_long
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: time
              after: '23:00:00'
              before: '05:30:00'
          sequence:
            - service: light.turn_on
              entity_id:  light.bathroom_1,light.toolbox,light.sink,light.tree_top
              data:
                brightness: 15
            - service: timer.start
              entity_id: timer.bathroom_motion_short

- 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
  action:
  - service: light.turn_off
    entity_id: light.bathroom_1,light.bathroom_2,light.toolbox
    
- 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:
  - service: light.turn_off
    entity_id: light.bathroom_1,light.toolbox,light.sink,light.tree_top

Got it. I think :wink:. Hereā€™s my understanding of the logic:

  1. Motion timer starts when there is motion - actions happen (ONLY lights turn on)
  2. Start a light timer (long or short depending on the time of day)

Then when those timers finish, the lights turn off. That seems pretty simple.

If a tag is scanned, then things get a little more complicated. At this point, Iā€™m assuming the motion sensor has activated and the long (or short) light timers have started. You want to turn on the lights and the heater, but the lights need to stay on longer. You cancel the motion sensor-activated timers, which is what I would have done, and start a heater timer called timer.bathroom_heat_tag.

Is that correct so far?

If the bathroom heater is already on and a tag is scanned, you finish a timer called timer.heater. Iā€™m not sure if thatā€™s another heater timer, or if itā€™s supposed to be bathroom_heat_tag. Iā€™m assuming you want to turn the heater off if itā€™s on (but Iā€™m not sure why that is?). Probably doesnā€™t matter for me.

So the goal at this point is to not have the light timers running, and only have the heater timer running. If thatā€™s the case, then the motion sensor is going to activate again at some point (itā€™ll quit seeing anyone and turn off, and then as soon as it sees someone again, it will turn on). If a tag has been scanned, then you donā€™t want the motion sensor to override the tag sensor.

Soā€¦ Iā€™d add a condition to the motion sensor automation that checks to make sure the heater timer is not ā€˜activeā€™. Something like this between the mode and action lines (this should also cover the case where the tag is scanned before the motion sensor triggers, if thatā€™s a possibility):

  condition:
    condition: template
    value_template: >
      {{ not is_state('timer.heater','active') }}

Hope I understand what you are trying to accomplish! If I messed up, let me know.

You hit the nail on the head! Yes, the timer.heater was a typo that I had to fix. It seems to be working perfectly, but I like the bit of logic you have added in the last post. The reason for the scan to turn off the heater is because we have cats and an unattended space heater running worries me.

I love the nuances between timer.cancel and timer.finish. This is a set of automations that I have never been happy with and now I am. No lights shutting off mid-shower! I had not used timers before, they really are you friend versus delays!

So, a big thanks for your insight and help.

1 Like

That makes a whole lot of sense now! This is the first time in decades we havenā€™t had a pet (or three) running around the house. That would definitely complicate my motion sensor logic! I hadnā€™t thought about thatā€¦

I post up my sensors to a point that they do not see cats down low and I add presence detection to a lot of my automations so they will not trigger if nobody is home. I use the Xiamoi sensors and the old round gateway and they work very well.

Got it - thatā€™s a great idea about positioning them above the floor level. I hadnā€™t thought about blocking them from turning on lights if weā€™re not home, too - excellent!

If we cave and end up with a COVID pet (is that a thing now?), Iā€™ll definitely have to do some logic-editing! Thanks for the thoughts!

No problem, good community. I have been running for years and really need to do some cleanup on old automations, some of them from the beginning are kinda scary, Overall I really love HA, but I have worked my entire life in networking and tech, when I croak the wife will just have to let it run until it quits. I am running on a Lenovo I3, tiny machine supervised docker install on Debian 10. Likely nearing 120 automations in 15 or so device focused directories.

It is fun!

Totally get it. My wife has said that, in case I die first, she wants instructions on how to disable everything :roll_eyes: