Problem with date stamps variable

how to put this in a automation ?
the goal is when statex is false I want a notification

states is true this moment, but when it not update anymore then statex will be false

{% set datex1 = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
{% set datez1 = now() %}
{% set statea = as_timestamp(datex1) | timestamp_custom (‘%H:%M’) %}
{% set stateb = as_timestamp(datez1) | timestamp_custom (‘%H:%M’) %}
{% set statex = statea > stateb %}
state sensor {{statea}}
state real time {{stateb}}

state senor is groter dan real time {{statex}}

output of states:
state sensor 09:02
state real time 08:30
state senor is groter dan real time True

state senor is groter dan real time {{statex}}

Put the template for statex in a binary template sensor and trigger when the sensor goes from ‘on’ to ‘off’

Or use a template trigger with reversed output logic:

{% set statex = stateb > statea %}
{{ statex }}

Also please format your pasted config correctly in future. Use the </> button in the post toolbar or this: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Hio Tom, Thanks for the info

i put this in the configuration.yaml :

template: 
     name: "Energy Day"
     unit_of_measurement: 'kWh'
     state: >
      {{ (states("sensor.apsystems_energy_day") | float) | round(2) }}       
     device_class: energy
     state_class: total_increasing

template: 
     name: "status sensor"
     unit_of_measurement: 'state'
     state: >
      {% set datex1 = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
      {% set datez1 = now() %}
      {% set statea = as_timestamp(datex1) | timestamp_custom ('%H:%M') %}
      {% set stateb = as_timestamp(datez1) | timestamp_custom ('%H:%M') %}
      {% set statex = statea > stateb %}   
      {{statex}}

en reboot HA
but i can not use te vallue “status senor” on a Card ?
“engery day” works good (as sample)

Your config is incorrect. I don’t believe any of those would work as written above. Please refer to the examples in the documentation

e.g.

template: 
  - sensor: 
      - name: "Energy Day"
        unit_of_measurement: 'kWh'
        state: >
         {{ (states("sensor.apsystems_energy_day") | float) | round(2) }}       
        device_class: energy
        state_class: total_increasing

  - binary_sensor:
      - name: "Status Sensor"
        state: >
          {% set datex1 = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
          {% set datez1 = now() %}
          {% set statea = as_timestamp(datex1) | timestamp_custom ('%H:%M') %}
          {% set stateb = as_timestamp(datez1) | timestamp_custom ('%H:%M') %}
          {% set statex = statea > stateb %}   
          {{statex}}

it works as I wanted :slight_smile:

When the device "states.sensor.vriezer_boven_power.last_updated " is to old, {{statex}} goes to False.
Based on that I can add it to my dashboard and build an automation with notification.

The Value “last_seen” Is nasty and ik takes to long for changing the status.
The other side of the story is, this way works only for devices that are on power 24/7 like a fridge or freezer.

So I made my fridge \ freezer smart by detecting there is no powerconsuming any more (even when the fuse is broken, by an other divice on the sam group)

Thank you for helping !!

If you’re interested, you can can simplify your template.

  - binary_sensor:
      - name: "Status Sensor"
        state: >
          {% set x = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
          {{ x.time() > now().time() }}
1 Like

Thankyou fr the beter code :slight_smile:

I paste it in the develop Template the value = False
my code = True

maybe because
x = 2025-02-10 19:02:50.920046+00:00
time now() = 19:07:00.060258

this is it …

  {% set x = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
        {{ x > now() }}

That’s not the template I had suggested.

Compare what you tried with what I posted above.

it don’t work I paste 1 on 1

  - binary_sensor:
      - name: "NPC_Demotwee"
        state: >
          {% set x = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
          {{ x.time() > now().time() }}

this wil work :

  - binary_sensor:
      - name: "NPC_Demodrie"
        state: >
         {% set x = states.sensor.vriezer_boven_power.last_updated + timedelta(minutes=60) %}
         {{ x > now() }}

Except it works differently from your original template. Your time comparison now also includes the date.

  • Your original template simply checked if a time (plus 1 hour) is later than the current time.

  • The latest template you created checks if a date and time is later than the current date and time. It’s similar but different.

For example:

  • 18:00 is later than 08:00
  • 18:00 yesterday is not later than 08:00 today.

To maintain the same kind of comparison as your original template, the template should explicitly compare time (not date).

ehhhhhhh

but with the time striped or full date your right but it works for me, Thanks again !

It’s working for you because the sensor’s last_updated value is today.

If you’re certain the value can never be later than today then it’s safe to use your version of my template.

However, it begs the question why didn’t your original template compare dates? Originally it only compared times.