Minutes untill next alarm - Error

Hi All,

Newbie here… I read the forum up and down and found many different ways to potentially solve this but I still can’t figure it out. In my configuration.yaml I have the following:

sensor:
  - platform: template
    sensors:
      minutes_next_alarm_v:
        friendly_name: "V Minutes Until Next Alarm"
        unit_of_measurement: 'min'
        icon_template: mdi:timer
        value_template: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp -             
         utcnow()|as_timestamp)/60)|round}}"

When I paste

"{{((states('sensor.v_s_21_next_alarm')|as_timestamp - utcnow()|as_timestamp)/60)|round}}"

in the Developer Tools/Template to test it out it works correctly (shows the exact time until the next alarm), but the problem is once the alarm is activated (reached) on my phone, in the Template result I get this error:

ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '"{{((states('sensor.v_s_21_next_alarm')|as_timestamp - utcnow()|as_timestamp)/60)|round}}"' but no default was specified

I read in many posts that I should use default=0 but I have no idea where?!?

Why is this and how do I fix it (do I need to fix it or this is expected)?

Thank you

You are using the old template yaml format. If you convert it to the new format (with template on top) you can set an availability template, so the sensor is unavailable when the’s no next alarm.

With the sensor as is, you could use a default parameter for as_timestamp to return the value that you want when there is no next alarm.

@Edwin_D

Thanks… could you please elaborate a bit more on

Should it be:

template:
  - sensor::
    - name: minutes_next_alarm_v:
        friendly_name: "V Minutes Until Next Alarm"
        unit_of_measurement: 'min'
        icon_template: mdi:timer
        value_template: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp -             
         utcnow()|as_timestamp)/60)|round}}"

Thanks

Close, but indenting and other details matter:

template:
  - sensor:
      - name: "V Minutes Until Next Alarm"
        unique_id: sensor.minutes_next_alarm_v
        unit_of_measurement: 'min'
        icon_template: mdi:timer
        availability: "{{ has_value('sensor.v_s_21_next_alarm') }}"
        value_template: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp -             
             utcnow()|as_timestamp)/60)|round}}"

Note that this will get a slightly different entity id based on the name.You can change it in the UI if you want.

@Edwin_D

So I pasted this into the Dev. tools/template

template:
  - sensor:
      - name: "V Minutes Until Next Alarm"
        unique_id: sensor.minutes_next_alarm_v
        unit_of_measurement: 'min'
        icon_template: mdi:timer
        availability: "{{ has_value('sensor.v_s_21_next_alarm') }}"
        value_template: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp -             
             utcnow()|as_timestamp)/60)|round}}"

And when the alarm on my phone has not reached the alarm state I get:

Result type: string

template:

  • sensor:
    • name: “V Minutes Until Next Alarm”
      unique_id: sensor.minutes_next_alarm_v
      unit_of_measurement: ‘min’
      icon_template: mdi:timer
      availability: “True”
      value_template: “1”

This template updates at the start of each minute.

This template listens for the following state changed events:

Entity: sensor.v_s_21_next_alarm

But when the alarm turns on, I get:

ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template 'template: - sensor: - name: "V Minutes Until Next Alarm" unique_id: sensor.minutes_next_alarm_v unit_of_measurement: 'min' icon_template: mdi:timer availability: "{{ has_value('sensor.v_s_21_next_alarm') }}" value_template: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp - utcnow()|as_timestamp)/60)|round}}"' but no default was specified

You should add it to your config.yaml, remove the old one and reload. And did you read the below remark that the entity is a bit different? your old one will still give the error, the new one should not due to the a ailability template.

@Edwin_D

I will try right now.

Thanks

@Edwin_D

I did add it to config.yaml and restarted and I get this error in the log:

2024-08-11 12:00:35.018 ERROR (MainThread) [homeassistant.config] 
Invalid config for 'template' at configuration.yaml, line 14: required key 'state' not provided
Invalid config for 'template' at configuration.yaml, line 17: 'icon_template' is an invalid option for 'template', check: sensor->0->icon_template
Invalid config for 'template' at configuration.yaml, line 19: 'value_template' is an invalid option for 'template', check: sensor->0->value_template

Thx

Ah, yes, I didn’t check everything you put in, I assumed you checked the docs. The value template should be state, _template should be removed for the ico, maybe some more small changes. Best to check this for what the fields should be called:

Ok, what worked is this:

template:
  - sensor:
      - name: "V Minutes Until Next Alarm"
        unique_id: sensor.minutes_next_alarm_v
        unit_of_measurement: 'min'
        icon_template: mdi:timer
        availability: "{{ has_value('sensor.v_s_21_next_alarm') }}"
        value_template: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp(0) -             
             utcnow()|as_timestamp)/60)|round}}"

I’m not sure if this is the new or old way but so far it is working

Thx

Update: spoke too soon…back to the drawing board :frowning:

Finally no errors :slight_smile:

template:
  - sensor:
      - name: "V Minutes Until Next Alarm"
        unique_id: sensor.minutes_next_alarm_v
        unit_of_measurement: 'min'
        availability: "{{ has_value('sensor.v_s_21_next_alarm') }}"
        state: "{{((states('sensor.v_s_21_next_alarm')|as_timestamp(default=0) -
             utcnow()|as_timestamp)/60)|round}}"
1 Like

Change icon_template to icon and that will work too.

Hi all,

So this is working fine,well sort of. It does what is supposed to do BEFORE the alarm but it also triggers AFTER the alarm?!

Here is the automation:

alias: Automation - Bedroom AC to 18C 30 min before V Alarm
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - zone.home
    above: 0
  - platform: numeric_state
    entity_id:
      - sensor.v_minutes_until_next_alarm
    below: 30
  - platform: numeric_state
    entity_id:
      - sensor.bedroom_thermostat_temperature
    above: 20
condition:
  - condition: zone
    entity_id: device_tracker.v_s_21
    zone: zone.home
  - condition: numeric_state
    entity_id: sensor.v_minutes_until_next_alarm
    below: 30
  - condition: numeric_state
    entity_id: sensor.bedroom_thermostat_temperature
    above: 20
    enabled: true
action:
  - action: climate.set_temperature
    metadata: {}
    data:
      temperature: 18
      hvac_mode: auto
    target:
      device_id: bb775957162c44f7362942d1f8f08e9d
mode: single

today it ran after the alarm was “below 30 and above 20min”. here is the part of the trace that ran the automation:

Executed: August 17, 2024 at 10:11:39
Result:
result: true

entity_id/0

Executed: August 17, 2024 at 10:11:39
Result:
result: true state: -11

condition: numeric_state
entity_id: sensor.v_minutes_until_next_alarm
below: 30

why does it run at -11 min when the condition is between 20-30min before the alarm? Also the alarm on the phone was off at that time (it was turned off at 10:03)

thank you

You keep saying “below 30 min and above 20 min” but the “above 20 min” part doesn’t appear anywhere in your automation

Dam… I was constantly thinking about below 30 and above 20 but never actually made it that way in the automation :man_facepalming:. Thank you!

But could that be causing the -11 to fire? Is it thinking that -11 is “below 30” and if yes then why not -5 or -15?

TY