Hi!
I’m giving my first steps with HA. One of the main features that caught my attention is the ability to create custom python scripts.
I’m trying to implement my first script with pyscript following the guide
I did all the first steps (see below) but I’m not finding anything in the logs, not even evidence that the service was even run or tried to run.
Given that I’m new, it may be something silly such as me not looking at the correct place for the logs, or a real issue with the script execution.
So I’d like to clarify that when I mean I’m looking at the logs, I’m looking at this file: /config/home-assistant.log
I’d appreciate any help
Steps I followed from the documentation
Create a file
example.py
in the<config>/pyscript
folder (you can use any file name, so long as it ends in.py
) that contains:@service def hello_world(action=None, id=None): “”“hello_world example using pyscript.”“” log.info(f"hello world: got action {action} id {id}") if action == “turn_on” and id is not None: light.turn_on(entity_id=id, brightness=255) elif action == “fire” and id is not None: event.fire(id, param1=12, pararm2=80)
After starting Home Assistant, use the Service tab in the Developer Tools page to call the service
pyscript.hello_world
with parametersaction: hello id: world
The function decorator
@service
meanspyscript.hello_world
is registered as a service. The expected service parameters are keyword arguments to the function. This function prints a log message showing theaction
andid
that the service was called with. Then, if the action is"turn_on"
and theid
is specified, thelight.turn_on
service is called. Otherwise, if the action is"fire"
then an event type with thatid
is fired with the given parameters. You can experiment by calling the service with different parameters. (Of course, it doesn’t make much sense to have a function that either does nothing, calls another service, or fires an event, but, hey, this is just an example.)Note
You’ll need to look at the log messages to see the output (unless you are using Jupyter, in which case all log messages will be displayed, independent of the log setting). The log message won’t be visible unless the
Logger
is enabled at least for levelinfo
, for example:logger: default: info logs: custom_components.pyscript: info