Timestamp maths in template sensor does not work

Hi,
I have an automation that gives me a notification if time since device last updated, exceeds a certain value.

- alias: timestamp detektor kjokken
  trigger:
    platform: time
    minutes: '/60'
    seconds: 00
  condition: 
    condition:  template
    value_template: '{{ (as_timestamp(now())-as_timestamp(states.zwave.brannvarsler_kjokken_5.last_updated)) > 21600 }}'

This works flawless.

But I also want my template sensor to show error status if this happens. That wasn`t as easy as I thought.
This does not work:

  branndetektor_kjokken_template:
    friendly_name: 'Branndetektor Kjøkken'
    value_template: >-
     {%- if is_state("sensor.brannvarsler_kjokken_smoke_5_4", "2") -%}
      Fire
     {%- elif is_state("sensor.brannvarsler_kjokken_smoke_5_4", "3") -%}
      Fire Test
     {%- elif (as_timestamp(now())-as_timestamp(states.zwave.brannvarsler_kjokken_5.last_updated)) > 21600 -%}
      Error
     {%- else -%}
      Ok
     {%- endif -%}

Does anyone know what I am doing wrong? Meaby use float in any way?
have tried this without any luck:

  branndetektor_kjokken_template:
    friendly_name: 'Branndetektor Kjøkken'
    value_template: >-
     {%- if is_state("sensor.brannvarsler_kjokken_smoke_5_4", "2") -%}
      Fire
     {%- elif is_state("sensor.brannvarsler_kjokken_smoke_5_4", "3") -%}
      Fire Test
     {%- elif float(as_timestamp(now())-as_timestamp(states.zwave.brannvarsler_kjokken_5.last_updated)) > float (20) -%}
      Error
     {%- else -%}
      Ok
     {%- endif -%}

This is working in template in Homeassistant, but not when I restart Homeassistant. Not any change.

  - platform: template
    sensors:
      branndetektor_kjokken_template:
        friendly_name: 'Branndetektor Kjøkken'
        value_template: >-
         {%- if is_state("sensor.brannvarsler_kjokken_smoke", "2") -%}
          Brann
         {%- elif is_state("sensor.brannvarsler_kjokken_smoke", "3") -%}
          Brann Test
         {%- elif float(as_timestamp(now())-as_timestamp(states.zwave.brannvarsler_kjokken.last_updated)) | float > (10) -%}
          Error
         {%- else -%}
  • platform: template
    sensors:
    branndetektor_kjokken_template:
    friendly_name: ‘Branndetektor Kjøkken’
    value_template: >-
    {%- if is_state(“sensor.brannvarsler_kjokken_smoke”, “2”) -%}
    Brann
    {%- elif is_state(“sensor.brannvarsler_kjokken_smoke”, “3”) -%}
    Brann Test
    {%- elif float(as_timestamp(now())-as_timestamp(states.zwave.brannvarsler_kjokken.last_updated)) | float > (10) -%}
    Error
    {%- else -%}
    entity_id: sensor.time

  • platform: time_date
    display_options:

    • ‘time’

Try it and it will work.
But entity_id option is deprecated, so I am looking at other way.

1 Like

That worked! Thank you so much :slight_smile:
So adding the entity_id, time, does that make the system check every minute if “float(as_timestamp(now())-as_timestamp(states.zwave.brannvarsler_kjokken.last_updated)) | float > (10)” is true?
Yeah, read that entity_id for templates is deprecated, so please keep me postet if you find a workaround.
Thanks again!

Edit: Only problem is that everything then just get updated once pr.minute. Which means that if fire-alarm goes off, it doesn`t get updated before sensor.time gets updated.

I am not sure but how about set a scan_interval for sensor.time?

Thank you, but set a scan interval to 2 seconds did not change anything. Could use a sensor that updates by seconds, but since that function will get deprecated, it is better to find another solution. But anyways, it is just esthetic, so it is not that important. But if you find a way, please update me :slight_smile:
Thank you for your help, anyways :slight_smile:

I found the other way.
The below is one of my sensor.
You can make a command line sensor which you can adjust the scan_interval

  • platform: template
    no_presence_time:
    value_template: ‘{{ (states.sensor.timestamp_now.state | int - as_timestamp(states.sensor.no_presence.last_changed)) | multiply(1 / 3600) | round(2) }}’
    unit_of_measurement: “hours”

  • platform: command_line
    name: timestamp_now
    command: “date +%s”
    scan_interval: 30

You are brilliant! That worked perfectly! Thank you so much! :smile: :smile:

Found out I got an error message because of this:

  - platform: template
    sensors:
      branndetektor_kjokken_template:
        friendly_name: 'Branndetektor Kjøkken'
        value_template: >-
         {%- if is_state("sensor.brannvarsler_kjokken_smoke", "2") -%}
          Brann
         {%- elif is_state("sensor.brannvarsler_kjokken_smoke", "3") -%}
          Brann Test
         {%- elif states.sensor.timestamp_now.state | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}
          Error
         {%- else -%}
          Ok
         {%- endif -%}

If I remove this part:

     {%- elif states.sensor.timestamp_now.state | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}
      Error

i got no errors anymore.

The error message during startup is this:

WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Brannvarsler Kjøkken, the state is unknown.

Do you know a workaround for this?

It is not a error just warning because the sensor.timestamp_now is not exist at the HA start for short time.
You can find the reason with the below.


(see the examples in the middle of page)

You may fix it with like the below.
{%- elif states(‘sensor.timestamp_now’) | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}

Thanks, I know it`s not a real error, just an irritating warning at startup.
Tried your solution, but the error is still there.