Help with Home Assitant app sensor.device_next_alarm template trigger

Hey all,

I was looking for some kind help And could t find my answer elsewhere at the forum.

I ran against an issue, probably Ill do something wrong just to mention i’am not an ICT related person just a computer hobbyist.

I’am using the Home assistant app (android) and tried to use the sensor.device_next_alarm to turn_on my bed light 10 minutes before alarm time for some reason this trigger template doesn’t trigger at 10 minutes before while the template editor works fine and shows as expected 10 minutes before alarm.“true”

Alarm triggers at the exact alarm_time.

I really tried everything to archive this

Thanks for coming by.

## Trigger lights 10 minutes before Alarm time  

UTC time             {{ utcnow() }}

UTC Unix time        {{ utcnow().strftime('%s ')|int +600 }}
Alarm time           {{states.sensor.mi_9_next_alarm.attributes['Time in Milliseconds'] /1000 -600  }}
Template             {{ utcnow().strftime('%s ')|int +600 >  states.sensor.mi_9_next_alarm.attributes['Time in Milliseconds'] /1000 |int }}
                            

Automation

- id: '1580845897584'
  alias: Wake_up Light Meta
  description: ''
  trigger:
  - platform: template
    value_template: '{{ utcnow().strftime(''%s '')|int +600 > states.sensor.mi_9_next_alarm.attributes[''Time
      in Milliseconds''] /1000 }}'
  condition: []
  action:
  - data:
      brightness: 180
      transition: 600
    entity_id: light.bedlamp_meta
    service: light.turn_on
  mode: single

It doesn’t trigger because utcnow() is a function, not an entity and Home Assistant assigns listeners to entities, not functions. That means the template is not evaluated every minute (like you’re expecting it to do) because Home Assistant is not “listening” for changes to utcnow().

I suggest you use the Time & Date integration to create sensor.time. It updates its state every minute and will cause a State Trigger to trigger every minute.

sensor:
  - platform: time_date
    display_options:
      - 'time'

After restarting Home Assistant, sensor.time will be created and can be used in automations, template sensors, etc.

Here’s how I suggest you use it. This automation will trigger every minute and then evaluate the template in the condition. If the condition evaluate to true it will execute the action.

- id: '1580845897584'
  alias: Wake_up Light Meta
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.time
  condition: 
  - condition: template
    value_template: >
      {{ now().timestamp() + 600 > state_attr('sensor.mi_9_next_alarm', 'Time in Milliseconds') / 1000 }}
  action:
  - data:
      brightness: 180
      transition: 600
    entity_id: light.bedlamp_meta
    service: light.turn_on
  mode: single
2 Likes

Hi Taras

Thanks for clear explanation about the “utcnow()” function good to know about that.
I used “utcnow()” because I could transform the data with example | timestamp_custom(’%H %-M’)

sensor.time_utc and sensor.mi_9_next_alarm data can’t be changed with timestamp_custom
at least for so far I know and sensor.mi_9_next_alarm state is is written in UTC time It also prevent summer time and winter time changes in my region

Slightly adjusted to my needs added extra 60 seconds to compensate the minute update frequency update interval and used UTC time instead GMT

{{ utcnow().timestamp() + 660 > state_attr(‘sensor.mi_9_next_alarm’, ‘Time in Milliseconds’) / 1000 }}

Solution provided by Taras post 2 cheers ! :+1:

2 Likes

Slightly modificated the code, so the alarm would go off only if the person is at home (based on devices geo - need’s home coordinates configuration and person/device assignment)

  trigger:
  - platform: state
    entity_id: sensor.time
  condition:
    - condition: and
      conditions:
      - condition: template
        value_template: >
          {{ utcnow().timestamp() + 660 > state_attr('sensor.mobile_device_next_alarm', 'Time in Milliseconds') / 1000 }}  
      - condition: state
        entity_id: person.person1
        state: home