First of all, @craigb, let me thank you for this great Addon to HA. In only a few days, it has completely changed my approach to automations and blown the scope of what I consider possible and doable. I was working with NodeRed before, and while it is capable of a lot of things, itβs just so tedious to figure out how to do something a bit more complex. With pyscript, itβs all super easy and straightforward.
I have one question, though, maybe I am to blind to figure it out by myself: can I create an entity from pyscript, that I can manipulate directly from Lovelace? Specifically, I would like to set up enable switches for my various automations.
Any variables withing the pyscript domain can apparently be displayed, but not modified in lovelace.
I tried to create input_booleans from pyscript, and they appear in the UI, but too, canβt be manipulated.
My pyscript functions work well but unfortunately they always run whenever I restart hass (so certain lights will go on or off when some people are sleeping). I would love to be able to stop this from happening. Anybody can give me some directions?
@state_trigger("switch.light_switch")
def switch_trigger():
task.unique("switch_trigger", True)
# DO SOMETHING
pass
Has anyone had luck getting autocomplete working for the Jupyter notebook in PyCharm?
Been banging my head against the wall all weekend without much luck. I have it working in the browser, but it makes development so much slower switching over to the browser. Iβll even take a guide on setting up VS Code, Iβm very unfamiliar with it, but it atleast supports IntelliJ key bindings. Had no luck with VS Code my first try but it was a fresh install and I had no idea how to set it up.
Iβm trying to create a MQTT watchdog in pyscript. It should be simple enough, just post a timestamp on something like watchdog/timestamp and then check it agains dt.timestamp(dt.now())β¦
Then I thought, maybe also use QOS and check if the message actually gets posted, but when I use mqtt.publish(topic=topic, payload=timestamp,qos=2) I donβt get an error or anything in the return of that function. I understand this is not really a pyscript issue, the strange thing is. If I try the same in the dev-console I do get an error saying it canβt connect to the mqtt broker (also with QOS=0).
So my question is, how do I catch an exception from mqtt.publish?
If the docs are right, that kind of trigger (just a state, without comparison) will not trigger on startup, but whenever the state changes (independent of the value). Can you check the state history? Maybe it gets initialized as βunavailableβ at startup and then the change to βonβ or βoffβ, once it becomes available, triggers your function.
Anyway, I ask myself why that would trigger unintended light switches, it sounds like a problem in the way you implemented the function itself. Maybe it makes more sense to define separate functions for on and off:
@state_trigger("switch.light_switch=='on'")
def switch_on():
task.unique("switch_triggered")
# DO SOMETHING
pass
@state_trigger("switch.light_switch=='off'")
def switch_off():
task.unique("switch_triggered")
# DO SOMETHING
pass
Like that, always the right state should be kept, even in case of restart (unless thereβs something more complicated at work here, which you did not explain).
Question. Is there a way using pyscript to manually change an attribute of a sensor. Iβm building a βto-doβ list and I have all the tasks as attributes of a main sensor and would like to change the status attribute to βcompletedβ when done since when is created is βopenβ. Thank you.
Hello everyone. I installed Pyscript using Home Assistant Community Store, and when I go to Developer Tools β Services β and type in pyscript, the only services that show up are Reload Pyscript and Start Jupiter Kernel. Iβve created more scripts in my custom_components/pyscripts directory, however they donβt show up in my services. Iβve restarted home assistant, made sure everything is correct in my configuration.yaml file, checked the debug statements, and cannot find what is wrong. Any advice would be great!
I followed the steps to create the example.py file, and tried creating a file of my own. I then called the reload scripts service and restarted home assistant. The new services, such as the services in example.py and the other file still wonβt show up. Pretty confused about what is happening.
<config> means the HA config folder, so you should have/create a pyscript folder where your HA configuration.yaml lives, not in custom_component/pyscript.
import requests
import sys
import time
from importlib import reload
if "/config/pyscript_modules" not in sys.path:
sys.path.append("/config/pyscript_modules")
import write_file
write_file = reload(write_file)
...
I tried adding write_file.py to /config/pyscript/modules and now get this error instead: Exception in </config/pyscript/shopping_list_sync.py> line 10: write_file = reload(write_file) ^ ImportError: module write_file not in sys.modules
I removed the reload() and the service shows up, thanks!
Actually didnβt realize I wasnβt on the thread where this script originated, but I now know Iβm not too far away from getting the script to work.
I just became aware of that script. It also contains several time.sleep(1) calls, which is a very bad thing since it blocks the entire HASS event loop. Please replace those calls with task.sleep(1).
Please also post on the script thread the fact that the reload() should be removed too.
My HA Configuration.yaml file is in /etc/homeassistant/config and the path for pyscript is /etc/homeassistant/config/custom_components/pyscript. If this is wrong, where should I move the pyscript folder. I appreciate your help!