Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

last_changed seems to have the current value?! #413

Answered by craigbarratt
andorardo asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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}')
You must be logged in to vote
1 reply
Comment options

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.

Answer selected by andorardo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /