Comments (14)
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.
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.
No, I don't. interesting. then how does NodeRed get that information? I get it using a GetCurrentState node.
from pyscript.
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.
@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.
@dlashua, I might be missing some import or something, because I'm getting this error:
NameError: name 'hass.states' is not defined
from pyscript.
@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.
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.
@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.
@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.
@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.
awesome. I'll install master and update you. thanks heaps
from pyscript.
works like a charm. thank you!
from pyscript.
Tested this as well. Works great!
from pyscript.
Related Issues (20)
- 2023.12 Beta - state.set not setting state HOT 1
- [Feature Request] Error/warning when function name is duplicated in code
- Even with supports_response, service is not returning anything HOT 2
- not implemented ast ast_generatorexp HOT 1
- can not import library after reboot of HA
- Expose home assistant jinja template functions HOT 1
- Croniter version conflict HOT 1
- "MQTT entity name starts with the device name in your config"
- Feature Request: Make apps configurable from Home assistant UI
- Feature Request: exclude from recorder
- attribute changes not detected
- "async with" statement fails without target variable assignment
- Newbie help running Python script under pyscript as a service HOT 2
- Translated state HOT 1
- Template evaluation HOT 2
- Exception in <file.pv_excess_control.on_time> line 246 HOT 2
- Issues using yaml module in pyscript HOT 2
- Python 3.11 support removed from Home Assistant HOT 4
- помогите с IMPORT в home asistant HOT 1
- PyRight LSP HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyscript.