Templating: Using variable in

I want to use a template to check if the state of an entity has been changed in the last 30 seconds in a condition. I derive the entity to be checked from another entity using another template.

Problem: I can’t figure out how to pass this to template for the condition. Here is a simplified version just setting the variable:

{% set rscovpos = "states.sensor.coverpos_roll_dach.last_changed" %}
{{ (as_timestamp(now()) - as_timestamp( rscovpos )) > 30 }}

If the states.sensor.coverpos_roll_dach.last_changed is inside the brackets it works (renders True), but as above it throws the following error:

TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

How can I pass a variable into the as_timestamp function?


Remove the double-quotes.

{% set rscovpos = states.sensor.coverpos_roll_dach.last_changed %}
{{ (as_timestamp(now()) - as_timestamp( rscovpos )) > 30 }}

Thanks, that got me a bit farther. My problem seems to be that I am using this template to deduce an entity out of another entity based on a fixed naming scheme:

{% set rsent = "sensor.roll_dach_electric_consumption_w" | replace( 'sensor.' , 'states.sensor.coverpos_' ) | replace( '_electric_consumption_w' , '.last_changed')  %}
          {{ rsent }}

{{ as_timestamp(rsent) }}

Now rsent renders to: states.sensor.coverpos_roll_dach.last_changed
BUT: {{ as_timestamp(rsent) }} renders to None

Nevertheless, this works:
{{ as_timestamp(states.sensor.coverpos_roll_dach.last_changed) }}

So it appears as the first template renders to a string`(which is logical, but I don’t know how to do the re-forming with taking away the “”), the second template cannot understand that it is an entity or it doesn’t get rendered after another?

I tried the same thing by rendering the first part in an automation (I deduce it from the automation trigger), then handing the entity_id over to a script, which was then supposed to render the template and do the timestamp check. Problem is that I get the same error as before.

So, here’s my actual question: How do I re-form the trigger entitiy_id in an automation as shown above to deduce another entity_id, which then to use in a script?

The following worked for me:

{% set rsent = 'sensor.roll_dach_electric_consumption_w' 
| replace( 'sensor.' , 'sensor.coverpos_' ) 
| replace( '_electric_consumption_w' , '')  %}

{{ expand(rsent) | map(attribute='last_changed') 
| list | join() | as_timestamp }}

If they are all sensor entities, whose object_id ends with _electric_consumption_w, then this should work:

{% set rsent = "sensor.roll_dach_electric_consumption_w" %}
{{ states['sensor.coverpos_{}'.format(rsent.split('.')[1][:-23])].last_changed.timestamp() }}

Thanks all for helping me out! In the end @Didgeridrew proposed a working solution with attaching the last_changed attribute to the variable.

Just FYI: @123 's solution did not work for me.

Now I have ONE single automation to monitor energy consumption on >30 roller shutters and refresh them only if after a run they did not report back a new position (as it happens once every 20 runs or so). Brilliant!

It worked perfectly in my testing so you must have done something wrong.