Unintended initial state change on automation run

Hi, can I please have some help with the start of this automation? I’ve tried setting initial_state to false, and also removing it completely.

My automation is a modification of the motion activated light blueprint, except that when motion is detected, it turns the light on at 20% brightness and changes its colour to red.

When I first tell the automation to run, with no motion in the room and with the light starting off, the light immediately turns on at a much lower brightness than the intended 20% and the colour starts out as either red or warm white. Then when I walk into the room to trigger the motion sensor for the first time, the brightness increases to 20% and the colour changes to red if it wasn’t red already. Then I can leave the room and the light turns off as intended, and I can repeat the process. It’s just the start of the automation that is misbehaving and I haven’t been able to figure out why.

Any advice or ideas would be greatly appreciated.

alias: Motion Nightlight - Hall 2 E
description: Motion Nightlight - Hall 2 E
initial_state: false
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hall_2_motion_e_motion
    from: "off"
    to: "on"
condition: []
action:
  - alias: Turn on the lights
    service: light.turn_on
    data:
      brightness_pct: 20
      rgb_color:
        - 255
        - 0
        - 0
    target:
      entity_id: light.hall_2_light_e
  - alias: Wait until there is no motion
    wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.hall_2_motion_e_motion
        from: "on"
        to: "off"
  - alias: Wait the number of seconds that has been set
    delay:
      seconds: 5
  - alias: Turn off the lights
    service: light.turn_off
    data: {}
    target:
      entity_id: light.hall_2_light_e
mode: restart
max_exceeded: silent

Possibly relevant side note: I used to have this automation trigger from either of 2 motion sensors. I can’t remember the starting conditions of the automation, but the first motion sensor caused the light to turn on at very low brightness, and the second sensor caused the light to increase to 20%. The only difference between my config with 2 motion sensors and the config below was that I had 2 motion sensor entities listed in the trigger definition.

Why? It’s used exclusively for enabling/disabling the automation on startup.

Reference: Automation Initial State

  • Setting it to true forces the automation to be enabled on startup.
  • Setting it to false will disable the automation on startup.
  • Without the option, on startup the automation’s state is restored to whatever it was prior to the restart (i.e. enabled or disabled).
alias: Motion Nightlight - Hall 2 E
description: Motion Nightlight - Hall 2 E
initial_state: false
trigger:
  - platform: state
    entity_id: binary_sensor.hall_2_motion_e_motion
    from:
      - "off"
      - "on"
    to:
      - "on"
      - "off"
condition: []
action:
  - if: "{{ trigger.to_state.state == 'on' }}"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 20
          rgb_color:
            - 255
            - 0
            - 0
        target:
          entity_id: light.hall_2_light_e
    else:
      - delay:
          seconds: 5
      - service: light.turn_off
        target:
          entity_id: light.hall_2_light_e
mode: single 
max_exceeded: silent

Thanks. This seems to fix the starting condition when running the automation. However, it can’t seem to turn the light off.

The dashboard shows the automation turning the light off, but the physical light is still on. However, I can manually toggle Hall 2 Light E from the dashboard very reliably so I don’t think it’s the light. I’ve also tested with Hall 2 Light W and that behaves in the exact same way.

Dashboard - Light not turning off.

Home assistant automatically reformatted your if statement condition, but otherwise the config I have is the same as yours.

alias: Motion Nightlight - Hall 2 E
description: Motion Nightlight - Hall 2 E
trigger:
  - platform: state
    entity_id: binary_sensor.hall_2_motion_e_motion
    from:
      - "off"
      - "on"
    to:
      - "on"
      - "off"
condition: []
action:
  - if:
      - condition: template
        value_template: "{{ trigger.to_state.state == 'on' }}"
    then:
      - alias: Turn on the light when motion is detected
        service: light.turn_on
        data:
          brightness_pct: 20
        target:
          entity_id: light.hall_2_light_e
    else:
      - alias: Wait a number of seconds after no motion is detected
        delay:
          seconds: 1
      - alias: Turn off the light if no motion is detected
        service: light.turn_off
        data: {}
        target:
          entity_id: light.hall_2_light_e
mode: restart
max_exceeded: silent

Change the value of mode to what I suggested.

Thanks. If I use mode: single, how would I make sure the automation is always running so that it doesn’t just trigger once? Should I use mode: queued instead, or am I misunderstanding the mode settings?

I tested with mode: single and the automation does continue running. I don’t understand why, but it worked. I think you solved my initial state problem so I’ll mark your comment as the solution. But I think I’ve found another problem from messing around with this.

If it’s not too much trouble, can you please also help me with this related post?