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

Can I have pyscript create a sensor/binary_sensor? #346

Unanswered
wigster asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

check the wiki. there are examples that should help.

You must be logged in to vote
2 replies
Comment options

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?

Comment options

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
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 によって変換されたページ (->オリジナル) /