How to make template sensor with as_timestamp update regularly?

I use this value as a condition for my Google Home alarm in automation.

{{ (as_timestamp(now()) - as_timestamp(strptime(states('sensor.bedroom_speaker_alarm'), '%Y-%m-%dT%H:%M:%S+00:00')))|int }}

It tracks how many seconds till alarm triggers. But I would like to make it a template sensor. When I use it as a value_template it doesn’t update. Only after restarting Hassio and then nothing. How should I make it update state?

1 Like

A template sensor needs some entity with state changes to update


You can use the Time & Date component

and add it to your sensor.

Thank you, it works. But can I make it update every second?

Sensors will show a last update when it’s output value changes but you can use

    force_update: true 

which will cause it to update when an input entity changes even if the output is the same.

It doesn’t work with template sensor. Where should I use force_update?

That’s not possible, i think.

Just realised that force update works with Mqtt sensors.

I think it is. There is entity_id variable - your sensor will update its state every time that entity changes state.
Not sure about using time directly, but if you create a input_boolean and an automation that toggles it every second (trigger: platform: time_pattern) and use that entity, it’ll do the trick

Or just read the second example here

And can you help me to solve my issues with timestamp errors like you did in this thread How to find config error?

I’m using this:
{% if (states('sensor.bedroom_speaker_alarm')) %} {{ (as_timestamp(now()) - as_timestamp(strptime(states('sensor.bedroom_speaker_alarm'), '%Y-%m-%dT%H:%M:%S+00:00')))|int }} {% else %} False {% endif %}

which works when alarm is set up but when it isn’t set up (sensor is unavailable), it doesn’t show False, only Unknown error rendering template.

This return ‘unknown’ if the entity is not availabe, so

{% if (states('sensor.bedroom_speaker_alarm')) %}

evaluates true.

Try it with:

{% if states.sensor.bedroom_speaker_alarm.state %}

You can force it to update with the homeassistant.update_entity service call. Calling that every second is left as an exercise :slight_smile:

1 Like

Unfortunately, still unknown error rendering template.


and i don’t have sensor.bedroom_speaker_alarm

I don’t know. Am I missing something?

If the sensor does not exist, states() returns “unknown” which evaluates to true in a condition.

What do you propose then? If my sensor is unavailable and template sensor with as_timestamp is active, it shows me too many errors like in this thread How to find config error?

Sorry, I missed that that had already been mentioned.

When you have the problem, print out the value with {{ states('sensor.bedroom_speaker_alarm') }} and go from there. Maybe you need something like this

{% if (states('sensor.bedroom_speaker_alarm')) != "unknown" %}
1 Like

I think the problem ist the template when the if evaluates true.

Unavailable instead of unknown and it finally works! Thank you!

yeah, unknown is returned when there is no such entity.
unavailable is returned when the entity does exist, but there is no definite state of the entity in question (i.e offline MQTT sensor/switch).