-
Notifications
You must be signed in to change notification settings - Fork 58
-
Hi,
My aim is to use the numerical state of a couple of HA entities, use pyscript to do some mildly complicated geometrical calculations on these states using numpy etc and produce a numerical output as a state of some entity. I would then use this value to automate things inside HA. Writing this in jinja2 as a template sensor is in principle possible, but would produce horrible code I'd never debug.
Is such a thing possible? Or would i need to bring all my automations which would be triggered on the value of this sensor into the pyscript script and have that run on some time-trigger? Or am I completely misunderstanding how this works?
To be concrete, imagine that I wanted to take the current states of a bunch of temperature sensors within HA and find the average temperature as a functions in pyscript and have it output as a sensor. This average temp sensor would be a drop-in replacement for the various triggers I have defined in the automations already based on the individual sensors. In reality it's more complicated, but this would be a good starting point.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 2 replies
-
check the wiki. there are examples that should help.
Beta Was this translation helpful? Give feedback.
All reactions
-
Im also looking for the same. I dont find any examples answering this.
How to do this?
data = "dummy"
entity = "sensor.mysensor"
state.set(entity, state = data)
Do i need to import anything?
Beta Was this translation helpful? Give feedback.
All reactions
-
After spending some time trying to understand examples, pyscript does exactly that. You can get the state of any entity as a variable, e.g. the Sun's posiiton
def get_sun_sph2(): return np.array([float(sun.sun.elevation),float(sun.sun.azimuth)])
And create a sensor by using state.set, e.g.
state.set(f"sensor.{cover.split('.')[1]}_optimal_tilt_angle",int(min_slat_angle[cover]),sensor_attributes['venetian']|{'friendly_name':f"{cover_name} Optimal Tilt Angle"} )
This then just creates a sensor.cover_optimal_tilt_angle in HASS, which you can treat as it if came from a usual integration. It's value is unknown until pyscript has been loaded -- that's the limitation. that can be circumscribed by having the sensors be created in the pyscript domain -- those are permanent.
YOu then need to have a trigger on some sort inside the pyscript code to actually update the sensor value, it is a decorator for the function which will be evaluated every time the trigger is triggered something like
@state_trigger("sun.sun.elevation")
def mufync
Beta Was this translation helpful? Give feedback.