Automation not turning off lights after presence is not detected

I have the following automation:

alias: Master Bathroom lights at midnight
description: Master bathroom lights will be dimmed at 15% from 11pm to 8am
trigger:
  - platform: state
    entity_id:
      - binary_sensor.humen_sensor_1m_8159b4_moving_target
    to: "on"
    enabled: true
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.humen_sensor_1m_8159b4_presence
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition:
  - condition: time
    after: "23:00:00"
    before: "08:00:00"
    enabled: true
action:
  - service: light.turn_{{ trigger.to_state.state }}
    data:
      brightness_pct: 15
    target:
      entity_id: light.master_bath_bathroom_sink_lights
mode: single

I would like for it to turn off my lights after no presence has been detected. I have the same automation for different hours and without setting the brightness levels and it does work. But when I add the brightness levels the automation does not turn off the lights. Any idea why? Is there something wrong with the code?

Any helps is appreciated,
thanks!

Change the service call as shown in the following example.

alias: Master Bathroom lights at midnight
description: Master bathroom lights will be dimmed at 15% from 11pm to 8am
trigger:
  - platform: state
    entity_id:
      - binary_sensor.humen_sensor_1m_8159b4_moving_target
    to: "on"
    enabled: true
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.humen_sensor_1m_8159b4_presence
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition:
  - condition: time
    after: "23:00:00"
    before: "08:00:00"
    enabled: true
action:
  - service: light.turn_on
    data:
      brightness_pct: "{{ iif(trigger.to_state.state == 'on', 15, 0) }}"
    target:
      entity_id: light.master_bath_bathroom_sink_lights
mode: single

That works,
thank you!

1 Like