Giter VIP home page Giter VIP logo

Comments (14)

craigbarratt avatar craigbarratt commented on May 18, 2024 1

Ok, I decided to support the virtual attributes last_changed and last_updated. Here's the documentation.

Use the master version to try it out.

from pyscript.

craigbarratt avatar craigbarratt commented on May 18, 2024

When you look in the UI in Developer Tools -> States, find the Entity you are interested in. Do you see any attribute or value there that has the information you need?

from pyscript.

shafai avatar shafai commented on May 18, 2024

No, I don't. interesting. then how does NodeRed get that information? I get it using a GetCurrentState node.

from pyscript.

craigbarratt avatar craigbarratt commented on May 18, 2024

I guess we'd have to look at the NodeRed code to see how it maintains that information. Perhaps it keeps track of the timestamps of all state changes?

Anyhow, you can do this manually in pyscript just by maintaining that information yourself. For example:

from datetime import datetime as dt

sensor_last_change = None

@state_trigger("binary_sensor.test1")
def binary_sensor_changed():
    global sensor_last_change
    if sensor_last_change is not None:
        time_delta = (dt.now() - sensor_last_change).total_seconds()
        log.info(f"binary_sensor.test1 is now {binary_sensor.test1}; it changed {time_delta} seconds ago")
    sensor_last_change = dt.now()

If you want to do this for multiple sensors, you could turn sensor_last_change into a dict indexed by the entityid, so you can maintain the last changed time for multiple sensors. If that doesn't make sense, I can reply with another example.

from pyscript.

dlashua avatar dlashua commented on May 18, 2024

@shafai the HASS State in HASS actually looks like this:

last_changed: some date here
last_updated: some date here
state: "the state"
attributes:
  friendly_name: "My Sensor"
  device_class: "motion"

Using the Home Assistant API, this structure can be seen with this call:

hass.states.get('binary_sensor.test')

I'm almost certain this is what NodeRed uses to compute that value. It's just these values are not being exposed by pyscript.

from pyscript.

shafai avatar shafai commented on May 18, 2024

@dlashua, I might be missing some import or something, because I'm getting this error:

NameError: name 'hass.states' is not defined

from pyscript.

dlashua avatar dlashua commented on May 18, 2024

@shafai sorry, that last bit was directed at @craigbarratt. The hass object is not generally available to a script in pyscript. So, at the moment, his comment represents the best solution to get this information. But @craigbarratt may be able to expose the value you are looking for in a future release.

from pyscript.

shafai avatar shafai commented on May 18, 2024

I see, thanks for clarification. that approach of keeping track of everything is too much state management for a small script. as in, I need to create a dictionary of states. I can certainly do it, but if the PyScript can provide it, it's so much better.
I can see AppDaemon does provide the information.
is there something I can do to help add this feature or I need to read how PyScript is bound to HASS?

I noticed pyscript is much faster to interact with entities in HA than AppDaemon...

from pyscript.

craigbarratt avatar craigbarratt commented on May 18, 2024

@dluashua - ah thanks. I do see now that the hass state object does keep timestamps of changes. Let me look into that and see how to expose them. Perhaps as a particular attribute name like change_time (although that would override a real attribute with the same name, which could still be available using state.get_attr())?

@shafai - the hass variable is available if you use the master version and set the hass_is_global configuration value to true.

from pyscript.

dlashua avatar dlashua commented on May 18, 2024

@craigbarratt that's the trouble. Because pyscript flattened the "attributes" level of the raw data (which makes it much less verbose when writing a pyscript) there will always be the worry of overriding something.

I think, at least in the case of last_changed and last_updated, enough people will want to use them that it's worth overriding. Especially when there is a work around available to get an attribute by the same name if there happens to be one.

The only other things in the HASS State object are context and object_id.

context is useful for linking events together. It also can tell you which user (or automation or whatever) requested the state_change you're looking at (see #50 for more information on how this can be used).

I'm not sure what object_id is used for. Perhaps it is unchanging even if the entity_id changes? I can't see a good use for this right now, though.

from pyscript.

dlashua avatar dlashua commented on May 18, 2024

@craigbarratt also, in case you fall down that rabbit hole, last_update and last_changed are both reset when Home Assistant restarts. last_changed is SUPPOSED to be the last time the state changed. But, I guess, on a restart, because of the way the sensors load, there's a state change in all of that.

from pyscript.

shafai avatar shafai commented on May 18, 2024

awesome. I'll install master and update you. thanks heaps

from pyscript.

shafai avatar shafai commented on May 18, 2024

works like a charm. thank you!

from pyscript.

dlashua avatar dlashua commented on May 18, 2024

Tested this as well. Works great!

from pyscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.