Need some templating/Jinja help!

I have some templates where I need to extract the last_updated from a templated entity. Any clues?

This is the entity template I want to extract the last_updated from:

light.{{ trigger.entity_id.split(".")[1] }}

But I’ve not been able to figure out how to get the state of this entity; and need to use it in a condition template to check the time between last update and now.

would actually result in something like light.light_livingroom or whatever your light name is. Just use the trigger itself.

No, the trigger is a different domain; it is a binary_sensor that corresponds to a light group to turn on.

I specifically need to get the state of the entity_id of the string mashup.

I’m sorry, I’m confused…

so you just need the entity_id.state? or you need to get the state of a light that has an attribute (name) associated to the binary_sensor?

ie binary_sensor.livingroom_light and light.livingroom_light?

Sorry; here is more context:

  trigger:
    - platform: state
      entity_id: 
        - binary_sensor.garage_auto_lights
        - binary_sensor.mudroom_auto_lights
        - binary_sensor.master_bath_auto_lights
        - binary_sensor.office_auto_lights
        - binary_sensor.family_auto_lights
        - binary_sensor.kitchen_auto_lights 
        - binary_sensor.exterior_door_night_auto_lights
        - binary_sensor.master_closet_auto_lights
        - binary_sensor.master_bedroom_auto_lights
      to: 'off'

  action:
    - service: light.turn_on
      data_template:
        entity_id: light.{{ trigger.entity_id.split(".")[1] }}
        brightness_pct: >-
          {{ states('sensor.target_brightness_inside')|int}}

What I want to do is add a condition that if the light was turned on (last_updated) over X minutes ago; so I know I can use the last_update for this but don’t know how to get that from the mashed up light entity above.

So, what you want is the last updated attribute of light.X when binary_sensor.X trips? I don’t think you can nest the brackets of the templates so I don’t think it’s going to be easy.

try

entity_id: light.{{ trigger.to_state.object_id.split('.')[1] }}

Correct, actually last_changed would suffice.

If it was easy I’d not be asking :wink:

@petro might have some magic?

Got it partially; this seems to get me the state object. Now to get the last_changed from it…

- condition: template
  value_template: >-
    {% set trig = trigger.entity_id.split(".")[1] %}
    {% set entity = ["light", trig] | join(".") %}
    {{ is_state( entity, 'on') }}
    - condition: template
      value_template: >-
        {% set trig = trigger.event.data.entity_id.split(".")[1] %}
        {% set entity = ["light", trig] | join(".") %}
        {{ is_state( entity, 'on') and (state_attr(entity, 'last_updated' == CALCULATION )}}

?

State_attr doesn’t seem to work on entity like you suggest; very odd.

I missed a close bracket, does that make any difference?

I’ve just realised, my lights don’t have a last_updated attribute :confused:

It’s tucked away elsewhere, so…

    - condition: template
      value_template: >-
        {% set trig = trigger.event.data.entity_id.split(".")[1] %}
        {% set entity = ["light", trig] | join(".") %}
        {% set updated = states.{{ entity }}.last_updated %} 
        {{ is_state( entity, 'on') and (updated == CALCULATION )}}

?

Mine have both; both the below return a valid date time object.

{{ states.light.mudroom.last_updated }}
{{ states.light.mudroom.last_changed }}

Yeah, but it’s not .attributes.last_updated there’s no ‘attributes’ in the call.

Does my second attempt do anything?

Nope. I can get to the state with my example; and I can get to real attributes using state_attr. But the two last_X values seem to be somewhere else I can’t figure out a way to access.

This Works:

{{ state_attr( entity, 'brightness' ) }}

OK, but when you use this one…

    - condition: template
      value_template: >-
        {% set trig = trigger.event.data.entity_id.split(".")[1] %}
        {% set entity = ["light", trig] | join(".") %}
        {% set updated = states.{{ entity }}.last_updated %} 
        {{ is_state( entity, 'on') and (updated == CALCULATION )}}

Does it generate anything for the ‘updated’ variable?

Nope, you can’t have the {{ nested inside the {% and it throws an error.

Same reason for the entity setup I use a dict and the join as using light_{{ trigger.entity_id.split(".")[1]}} would also throw an error due to the nesting.

But using a join to get states.light.mudroom.last_changed doesn’t work

It really seems these live somewhere between state and state attributes and some trick must exist to get to them.

Yeah, I can’t work out why last_updated isn’t an attribute tbh, but I’m running a blank now, sorry.

  {% set domain, object_id = trigger.entity_id.split('.') %}
  {{ states[domain][object_id].last_updated }}

None of the methods, i.e is_state(), states(), state_attr() return a states object. The only way to access a states object is via the states machine which typically looks like states.domain.object_id (i.e. states.light.livingroom). This gives you the state object and you will now have access to all properties of the state object. The other way to access states machine objects is states[domain][object_id] or in the example states['light']['livingroom']. You can mix and match too, i.e. states.light['livingroom'] or states['light'].livingroom.

Anyways, here is more info on the states object:

2 Likes