last_changed seems to have the current value?! #413
-
Hi, I'm loving pyscript, but having trouble using the I'd like to know how long since the sensor.xyz changed: @state_trigger('sensor.xyz')
def xyz_change(**kwargs):
last_changed = sensor.xyz.last_changed
log.info(f"last_changed {last_changed}") But from what I can tell, the last_changed value is always extremely recent (< 0.02 seconds ago), no matter what the last changed field shows in the developer tools. My guess is that it's showing the attribute for the new, current update Is this working as intended? Is it possible to get a hold of the 'from' state? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Because the function call is executed immediately after the trigger, the state variable has just been updated, and, as you note, the You can get the @state_trigger('sensor.xyz')
def xyz_change(old_value=None, **kwargs):
last_changed = sensor.xyz.last_changed
log.info(f"last_changed {last_changed}")
if old_value is not None:
log.info(f' old_value {old_value} last_changed {old_value.last_changed}') |
Beta Was this translation helpful? Give feedback.
Because the function call is executed immediately after the trigger, the state variable has just been updated, and, as you note, the
last_changed
is therefore very recent.You can get the
old_value
(inkwargs
) and use itslast_changed
attribute (although the first time,old_value
will beNone
):