-
Notifications
You must be signed in to change notification settings - Fork 58
last_changed seems to have the current value?! #413
-
Hi, I'm loving pyscript, but having trouble using the last_changed
attribute.
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.
All reactions
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
(in kwargs
) and use its last_changed
attribute (although the first time, old_value
will be None
):
@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}')
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 last_changed
is therefore very recent.
You can get the old_value
(in kwargs
) and use its last_changed
attribute (although the first time, old_value
will be None
):
@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.
All reactions
-
👍 1
-
Thanks! It's good to know that "the function call is executed immediately after the trigger", and the last_changed value has already been updated.
Beta Was this translation helpful? Give feedback.