Check if time is equal to a sensor

Hey everyone,
I am struggling to use my alarm clock as a trigger for home assistant. I want my lights to turn on 10 minutes before my alarm goes off. My android alarm is linked to home assistant via the ariela app and is a separate sensor. This is what I tried so far but I am not that great at YAML automations.

- alias: Wake up light
  trigger:
    platform: template
    value_template: {{ now() | string == sensor.poop_next_alarm }}
  action:
    service: homeassistant.turn_on
    entity_id: light.xxx

according to home assistant there is an invalid key at the value_template line. Does someone have any ideas?

try this:

- alias: Wake up light
  initial_state: true
  trigger:
    - platform: time_pattern
      minutes: '/1'
      seconds: 0
  condition:
    - condition: template
      value_template: '{{( as_timestamp(now()) >= (as_timestamp(states("sensor.poop_next_alarm")) | int - 600 ) )}}'
    - condition: template
      value_template: '{{( as_timestamp(now()) < as_timestamp(states("sensor.poop_next_alarm")) | int )}}'
    - condition: state
      entity_id: light.xxx
      state: 'off'
  action:
    - service: light.turn_on
      entity_id: light.xxx

Note: This assumes sensor.poop_next_alarm returns a datetime value
If it returns something else, please specify

Thank you, it seems to work

1 Like