Why can’t I pass the actions variable to the sequence? It’s like it’s forcing me to define actions in the code - which defeats the whole purpose of this script…
Hmm, that’s intriguing
TBH, I have zero experience with writing HA python scripts as services. Will have to do some digging. Any useful links will be appreciated (already found official docs).
Can you explain how this differs from automations?
I’ve seen automations which allow passing entire actions as their arguments; meaning something like:
Why does that work, but variables in a script do not? Obviously the implementation in automations is not a straightforward template (using !input etc.), but I don’t understand how it differs under the hood…
That’s a blueprint, not an automation. You can’t pass full actions in automations. Only blueprints.
Again, blueprint.
Blueprints create automations based on what you select for the blueprint. There’s a dedicated blueprint UI that does all of this for you. In the end, you end up with an automation. That automation will not contain !input and will not have variable actions.
Apologies, I did mean blueprints. And I see your point.
I think maybe the confusion comes from the fact the front-end usage looks very (very!) similar in both use-cases: user is given form-fields to provide data, which can be used in the configuration as variables.
in blueprints, it’s inputs that define form-fields for data to be used in the action section;
in scripts it’s fields that define similar form-fields for data to be used in the sequence section.
Follow-up question: assuming I’ve created a python script service (challenge accepted ), how do you suggest implementing a script to handle arbitrary actions?
Reading the docs, I see how I can use variables passed to the script, to call services for example. But arbitrary actions can come in lots of different forms, (service, variables, while, delay, wait_for_trigger, choose etc.); is there an execute_action()- type callback, or would I need to handle each action type separately?
all service calls are the same in the home assistant api, the data passed is different. The data is a dictionary, so just add the appropriate keys when needed. You can treat it as a pass through.
I’m looking for a better solution to creating actionable notifications then what I’ve found so far.
The best I’ve found was @vorion’s implementation with a blueprint; this simplifies the action(s)-definition and event handling, but means you’d create multiple automation definitions, one for each notification scenario. That seems redundant IMHO.
I’d like to be able to create these actionable notifications on-the-fly.
Things I need to code (I think):
create a script with fields similar to the inputs in the blueprint above
the script will take care of all the notification-creation logic- calling the service, passing title/message/action title(s) etc., waiting/receiving for the notification event…
… at that point it will pass the received user-choice and possible actions to python script for execution
End goal- to be able to simply call script.actionable_notifications, pass title/message and actions to it, and let it dispatch the notification.
so seems like the error is the same; can’t have a template as a sequence.
Then tried your suggestion #2, was able to save it as a script.
However, can’t use this in the GUI; only way to pass a list of services would be via YAML code, as far as I understand.
@petro
The solution I found was indeed using a python script. It turned out the most flexible.
It can handle service calls which use either target or data as their parameter(s).
Thanks for the suggestion.
/config/scripts.yaml
my_script:
alias: Send (an unknown number of) various actions to be executed
mode: single
fields:
actions:
name: 'Service call(s)'
required: true
default: []
selector:
action:
sequence:
- alias: Send the actions to python script
service: python_script.actions_execute
data:
actions: "{{ actions }}"
/config/python_scripts/services.yaml
actions_execute:
name: Execute actions
description: Execute the chosen actions
fields:
actions:
description: The actions to execute
/config/python_scripts/actions_execute.py
def get_param(service):
if "data" in service:
return service["data"]
if "target" in service:
return service["target"]
return null
actions = data.get("actions")
for service in actions:
service_name = service.pop("service")
domain, name = service_name.split(".")
param = get_param(service)
hass.services.call(domain, name, param)