Automation to turn light on depending on sensor values

Hello.

I have 2 sensors that receive my morning alarm time and morning alarm time + 15 (wake up time) via API and I’d like to be able to have an automation that if time is >= morning alarm time (sensor.wake) and time <= wake up time (sensor.wake_up) and light is off, it would turn on this light with specific settings.

This is what I currently have on my UI:

And this is my automation (which doesn’t seem to work:

- alias: 'Morning light'
  trigger:
    platform: template
    value_template: '{% if is_state("light.papa", "off") %}true{% endif %}'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states("sensor.wake_up") > now().time().strftime("%T") }}'
      - condition: template
        value_template: '{{ states("sensor.wake") < now().time().strftime("%T") }}'
  action:
    - service: light.turn_on
      entity_id: 'light.papa'
      data:
        brightness_pct: 3
        rgb_color: [255, 153, 85]
    - service: notify.iOS
      data:
        message: 'Morning Light turned on'
        title: 'Home Assistant alert'

Already tried using trigger platform state ‘off’ and it also doesn’t work.

If I trigger the automation manually from the UI, it works properly, but not automatically as needed.

Any insights are gladly accepted.

Thanks in advance for any insights.

Triggering on the state of the light is probably not what you want to do here.
I would flip these around and trigger off the first time, then use the light and the second time as the condition.
there’s a long thread here here and about 2/3 of the way down they hit on the idea of triggering against the time like this:

trigger:
  platform: template
  value_template: '{{ now.time().strftime("%-H") == states.input_slider.slider_houres.state and now.time().strftime("%-M") == states.input_slider.slider_minutes.state }}

I’ll give that a try, then.

Thanks a lot.

UPDATE: No luck… Here’s what I tried:

- alias: 'Morning light'
  trigger:
    platform: template
    value_template: '{{ states("sensor.wake") < now().time().strftime("%T") }}'
  condition:
    condition: template
    value_template: '{{ states("sensor.wake_up") > now().time().strftime("%T") }}'

At the end, using time entity instead of now().time() was what did the trick.

Here’s the final working config:

- alias: 'Morning light'
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.wake.state }}'
  action:
    - service: light.turn_on
      entity_id: 'light.papa'
      data:
        brightness_pct: 1
        rgb_color: [255, 153, 85]
    - service: notify.iOS
      data:
        message: 'Morning Light turned on'
        title: 'Home Assistant alert'

Thanks for all the help anyway.

1 Like