Pyscript! are you using it?

Hi @craigb,

Thanks a lot for your answer. To be honest I think I will live with the unhandled exceptions in the log files as your proposed workaround would compromise quite a bit the code readability as the triggers that I am using are rather lengthy on their own.

But this conversation could be used as a foundation for a feature request on pyscript. I am unaware how hard would it be to implement though.

you can also create the condition as a string:
eg:

condition1="pyscript.test1 is not None and float(pyscript.test2) == 1"
condition2="pyscript.test2 is not None and float(pyscript.test2) == 2"
statecondition=condition1+ " or " +condition2

@state_trigger(statecondition) def func():
  print("function called")

I do this when my trigger conditions get lengthy…

(just an idea :slight_smile: )

you can even simplify those with some basic formats:

float_check = "{0} is not None and float({0}) == {1}"

statecondition = ' or '.join([float_check(f'pyscript.test{i+1}', i+1) for i in range(2)])

It pays to learn how to format strings in python.

Google search for “python f string format” and “python string format” to learn all out f strings and string formating to make repeated conditions easier.

Another way

entity_ids = ['pyscript.test1', 'pyscript.test2']
values = [1, 2]

statecondition = ' or '.join([f"{entity_id} is not None and float({entity_id}) == {value}" for entity_id, value in zip(entity_ids, values) ])

sure, you can do it like this… just wanted to show, that you don’t need to specify the whole condition in the @state_trigger condition itself, but using a variable is also possible.
I find it more readable and easier to maintain. Your solution is more advanced and certainly a solution I’m going to use later (I knew the f formatting), but didn’t use it with List variables yet… Thanks!

Well, it’s more for F strings tbh. You can just copypaste code and only ahve to edit 1 thing. Here’s the simplest form:

entity_id = 'sensor.xyz'
value = 1
condition = f"{entity_id} is not None and float({entity_id}) == {value}"

Then just copy/paste to your hearts content and you only have to edit entity_id once and you don’t need quotes around the value.

It’s just little things like that, that can save you time.

Yes, i understand :smiley:

(I know that this is a 2 years old thread but I could not resist …) Oh yes, I am using it. It is FAN-TA-STIC.

I started with HA some 2 or 3 years back, first with the built-in automation and almost gave up - programming in YAML is really hard :slight_smile:

I then discovered App Daemon which is really nice, used it for a year or so, and stumbled upon it someday (I do not remember how, maybe Hacker News, but I seem to recall I saw something from @swiftlyfalling).

Oh man, how wonderful it is to use that instead of the built-in automation. I actually tried a few times to use them (as HA was moving ahead) but quickly hit a wall when trying to do something even slightly more complicated. The only thing I am missing (but this is really a nice to have) is a view of the automations in HA (hardly possible, I know).

Not to mention the great support in the GitHub discussions.

Thank you for that @craigb, and everyone else who contribute or help users!

1 Like

My evolution was very similar to yours. I came from Indigo where if I wanted custom functionality I had to write Python, so it was natural to want to do that in HA (and I didn’t understand the YAML implementation of scripting yet). My first stop was App Daemon but it couldn’t do everything I wanted so I switched to Pyscript and it was just like I was used to.

Now I have learned enough about scripting in YAML that I only have one relatively complex thing left in Pyscript but am writing a custom component for it now so I can not have to worry about that script.

1 Like

Oh yes I’m using it ! I have almost all my native HA automations converted to pyscript at this point, only a couple of very simple ones left. As a software engineer, having all my automations in code is the most intuitive and efficient way to me. It just speaks to me so much more than, for example Node Red, which is just a huge convoluted mess of blocks and lines to me :yum: And the HA native automations are just so limiting and awkward.

So thanks a lot for pyscript, saved my day !

1 Like

Hi guys, long time no see you.

Is there a way to implement an simple CI/CD pipeline (Github actions or similar) into the pyscript workflow in order to have the automations in a git repository and update HAS with commits into the repo, otherwise is quite a bit of work keeping up to date the two codebases in the repo and in the HAS server…

I guess the simplest solution is to create a git repository in HAAS and keep pulling

Anybody know how to listen for any “binary_sensor” event with pyscript?
I can only figure out how to listen to a specifically named sensor.

Thanks!

You could create a trigger off EVENT_STATE_CHANGED events, conditioning it on the entity (state variable) name. See this wiki page.

1 Like

Thanks @craigb. I’ve been using the docs. Didn’t even realize there’s a wiki too. :+1:

Here’s where I landed with this.

from homeassistant.const import EVENT_STATE_CHANGED

@event_trigger(EVENT_STATE_CHANGED)
def monitor_magic_area_events(entity_id=None, old_state=None, new_state=None):
    if "binary_sensor.area_" in entity_id:

        area = entity_id.removeprefix("binary_sensor.area_")
        areas_to_ignore = ["global", "interior"]

        if area not in areas_to_ignore:
            old_value = old_state.state if old_state else None
            new_value = new_state.state

            log.info(f"{area} changed from {old_value} to {new_value}")
1 Like

@craigb I was wondering if you have any good example on how I could create “dynamically generated functions”? The idea is for an function to run on startup, find all device classes == battery and then dynamically generate and register an function decorated with state_trigger for each device.

The main goal is to to instantly get notified when battery below X, and to avoid manually listing each battery device. I could create an function which run at cron x,y - however less ideal in my case

I’m trying to learn so I’m hoping for some directions on what I could look at to achieve this :slight_smile:

I have done some of this, to create trigger functions for thermostats. Not sure that it is the most elegant, but it works! TBH, still learning pyscript, but I have reproduced a massive Node-RED flow that controls our central heating with many zones. I have a trigger factory function, but am not doing discovery to find devices of the correct class. Hope it helps.