Template works in dev-template but not in UI

I have the sensor below.

If I just test {{(as_timestamp(now())-as_timestamp(states.group.motion_indoors.last_updated)) | round(0)}} in the dev-template console, it works. However when I use the sensor below it does not work correctly in the UI (just stays at 0s). See screenshots below.

Any ideas?

  - platform: template
    sensors:
      last_motion:
        friendly_name: lastmotion
        value_template: '{{(as_timestamp(now())-as_timestamp(states.group.motion_indoors.last_updated)) | round(0)}}'
        unit_of_measurement: 's'

The UI:

The dev-template output:

Might be totally wrong, but I’m wondering if the quotes are asking HASS to treat your output as a string, which results in a zero. What if you try:

- platform: template
    sensors:
      last_motion:
        friendly_name: lastmotion
        value_template: >
          {{(as_timestamp(now())-as_timestamp(states.group.motion_indoors.last_updated)) | round(0)}}
        unit_of_measurement: 's'

Like the idea! But no go, similar result

Ty, do you get any errors or does it just not work?

Nope, no errors (beside the annoying async zwave errors)

I have found another way to get this working based on the post below.

@ReneTode Do you have any idea why above situation occurs?

yeah, you need something that triggers the update.

look at this code

sensors:
  - platform: time_date
    display_options:
      - 'date_time'
  - platform: template
    sensors:
      controle_sensor:
        friendly_name: any sensor
        value_template: '{% if states.sensor.your_sensor.last_updated is undefined %}{{"00:00:00"}}{% else %}{{ ((as_timestamp(states.sensor.date__time.last_updated)-as_timestamp(states.your_sensor.last_updated))|timestamp_utc)[11:19] }}{% endif %}'
        entity_id:
          - sensor.date__time

the sensor.date_time is the keyfaktor. that makes that your sensor gets updated every minute.

2 Likes

I’m happy I have a working solution but I conceptually struggle to understand. Why would the original example not work? By defenition you would expect the sensor to update as the now() is a state change.

Is this a HASS design flaw?

a sensor (including templatesensors) are updated at a certain time.

the entity you give in the template sensor is needed to update the template sensor.

you like your templatesensor to update as regular as possible, thats why you use the date_time sensor as base entity.
you could also use your motion sensor as base entity for your template but then it would only update when your motion was updated.

now() is not a sensor, so that isnt updated at all. now() has no event. it is just a calculation.

to get the time event in HASS you Always need the date_time sensor. it is created for that i think.

2 Likes

Thanks Rene! Really did not know this and don’t think this is documented anywhere. When you explain it, it makes sense.

Last Q:
Why is there a double underscore with date__time? (and not just date_time ?)

i think that documentation in HASS fails on more then one place :wink:
i would like to change that but for that is my english not good enough.

in some places entitynames are rewritten. spaces, dots and some other characters are changed to underscores.
sometimes that gives a double underscore.
i dont know the code behind the date_time sensor, but i think that the double underscore comes from that.

if i remember correct a single underscore can sometimes be replaced by a double underscore to know afterwards that a single one was originaly there. so that by retranslating the single underscore gets translated to a space and the double underscore get translated to an underscore.

in py it is date_time so i think thats why.