I cannot find a good answer for this. I’m looking to extract the old state info from an entity state change within jinja template. Google says I can use {{ state_attr(‘entity_id’, ‘old_state’) }} but the value is null. I am specifically trying to get the attributes of the old_state, i.e. old_state.attributes.min_color_temp_kelvin.
hi Bob,
That’s part of a state_changed event and you have to capture that event to see this happen.
Values in events don’t hang around. You have to be listening when they happen, so likely need a triggered template I would guess.
Trigger on the event, and then grab the entity, make sure it’s the right entity, then save your state in the template.
You’re gong to need to clarify what you are trying to do. In most case templates have no access to that data. The only time a previous state is available for use in a template is from a trigger. Using a State trigger is the better option than an Event trigger in most cases.
Google (especially Gemini) is dumb… old_state
is not a valid attribute for any entity I’m aware of.
Are you sure? AFAIK, the attribute min_color_temp_kelvin
is static.
I was going to say AI hallucinations as well, but old_state, new_state is a thing in a state changed event, and they could get it if they wanted to. It would be of no use, most likely, but it’s a thing. That is a link to the state_changed event above.
Yeah, but it’s way less efficient to use an open Event trigger that listens for all state-changed
events than to use a State trigger. And you have to use an open event because there’s no feasible way to limit the trigger further than event_type
.
Agree, and the from is in the state trigger as a ‘this’ variable.
You’re gong to need to clarify what you are trying to do.
I’ll try to explain as best as I can, I’m on my phone right now so I can’t really get give you my .yaml.
I have an automation that detects when a new entity is added to a list and meets certain criteria; specifically it detects when a new light with a circadian label turns on then runs the circadian macro. The automation uses 2 template sensors, one that creates a list of lights that have the circadian label and are ‘on’, then another that determines which new light turned on by comparing the from_state to the to_state. The second template triggers the automation which runs the macro.
This all works fine, however, there are times when I don’t want the light to run the circadian macro. We could just say turn "on/off circadian " then give what new values we want, but it seems cumbersome in form. I’d like it to run the circadian macro automatically UNLESS someone directs to do something else (.i.e. “red”, “day light”, 100% brightness). The best way I can think to do this is to compare the light’s old/new state; if the old brightness and color/temp values are the same as the new, then no one specified a specific value they wanted the light and it would run the circadian automation. if not the same, then someone specified a specific value and don’t run it.
As the automation uses a trigger value that is never the same light, it’s ‘from_state’ cannot be used to get this info. I was hoping to be able to get the lights ‘entity_id’ from the automation then use that in a conditional template to compare the old and new states.
Yes it is. None of the documents it referenced actually even referenced {{ state_attr(‘entity_id’, ‘old_state’) }}.
This was the path from the node-red state change. I honestly LIKE building automations in node-red better, I feel like you can do more, but it just seems like one more point of failure so I’d RATHER build them in the native automations.

As the automation uses a trigger value that is never the same light, it’s ‘from_state’ cannot be used to get this info.
This doesn’t make sense. A state change event only shows a single entity in the to_state and from_state. So you’re either not using a state trigger, or you’ve made an incorrect assumption
the state change triggering the automation is the template sensor’s state change. The template sensor’s value is the entity_id of the new light. Since the template sensor’s value is always a new light, I can’t use the last_state of that trigger to get the state_change info of the light itself.
I really wish you could use more dynamic state triggers than are currently available; i.e. if any entity with a certain label triggers, then trigger automation as if that entity triggered it. Or a better state_change event trigger that can regex or pick entities like the node res one. I think I might have to move to Node Red on this one; I’m not sure what I want to have happen will work with native automations.