Getting previous attribute values after a state change

I’m currently looking to capture whenever I (not an automation) change the state of my TRVs - for example from auto to off. This changes the “hvac_mode” attribute, and is of course really easy to use as a trigger.

However, as a part of the automation, I want to capture the previous state of a different attribute - “temperature”. This corresponds to the setpoint of the TRV, and I’m wanting to store what it was if the HVAC Mode was “heat” - aka manual.
The problem is, when the TRV is in auto mode, the “temperature” attribute reports the setpoint in the TRV’s schedule, and when in “off” mode the attribute reports the frost protection temperature.
What I’m looking to do is capture the “temperature” attribute as it was before the HVAC Mode changed, so that if the previous mode was “heat”, I can store the setpoint in a variable for later use (some automations automatically change the HVAC Mode and temperature setpoint).

Any ideas?

Assuming you’re using state triggers, trigger.from_state.state and trigger.from_state.attributes. If you are triggering on a state_changed event, trigger.event.old_state.state and trigger.event.old_state.attributes.

I take it those would be what I can put in templates to get their values?

Yes. But they will of course only work in the automation which received the trigger. There is as far as I can tell no way to programmatically look up previous states outside of triggers, at least if only using HA Core without some third-party extension.

Perfect - my plan is to store the values in number input helpers, essentially “global variables” so I can use them later in other automations… many thanks for the pointer!