ok, I am a noob regarding HomeAssistant.
I managed to create the script. But how do I connect an automation to this script? You mentioned a service- how can I create a service? I read the documentation about service but still no clue. “Use the “Services” tab under Developer Tools to discover available services.” is written there.
But it remains unclear how to create own services as you suggested.
Thanks!
If you don’t use packages, you can create a new script in the UI script editor. Click on „create“ and then on the 3 dots top left to switch into yaml mode. Delete the few lines you see and copy everything except the first 2 lines (script: + auto_an_szenen: ) into the editor window, press save.
I have the script created.
I want to create an automation to use this script.
An automation can not directly start a script (as far as I got it) but instead can call a service.
A service itself appears to be able to execute a script. Execute this script.
But how do I create a service to execute the script in an automation?
Let summarize what I have currently:
As script I have:
alias: auto_an_szenen
description: |-
Aktiviert eine bestimmte Szene in allen Räumen, in denen
bereits das Licht brennt (beschränkt auf Hue)
——> /packages/skripte/auto_an_szenen.yaml
variables:
ignore: []
name: Energie tanken
szene: |-
{%- set x = namespace(szenen=[]) %}
{%- for hue in states.light
|selectattr('attributes.is_hue_group', 'defined')
|selectattr('state', 'eq', 'on')
|map(attribute='object_id')
|reject('in', ignore)
|list %}
{%- set x.szenen = x.szenen + ['scene.' ~ hue ~ '_' ~ name|slugify]
|list %}
{%- endfor %}
{{ x.szenen }}
sequence:
- if:
- condition: template
value_template: "{{ szene != [] }}"
then:
- service: scene.turn_on
target:
entity_id: "{{ szene }}"
else:
- stop: Lampen sind bereits eingeschaltet
mode: single
icon: mdi:sun-angle
As automation I have:
alias: A Alles nach Sonnenaufgang aus
description: ""
trigger:
- platform: sun
event: sunrise
offset: "0"
- platform: time
at: "22:30"
condition: []
action:
- service: script.auto_an_szenen
mode: single
When I execute the automation manually I am getting an error (German):
Die Automatisierung "A Alles nach Sonnenaufgang aus"
(automation.stall_strahler_aus) hat eine Aktion,
die einen unbekannten Dienst aufruft: script.auto_an_szenen.
stall_strahler is the first light item which mould be executed…
So is the service configuration needed or not? Really confused…
My fault, thought to much in packages. When using packages, the entity id is same as the the script name. When using the UI editor you have something like script.neues_skript. Please check your scripts.yaml or the entities overview.
And now I’ve encountered the reason: Your scripts.yaml starts with script: So HA handled it as entity_id of the script (scripts.yaml may not have a domain prefix). If you want to alter the entity_id, change script: to something you prefer ( for example sonnenaufgang:)
Additional question regarding the script and script programming in HoAs in general:
The script you provided switches all lamps which are no off to the configured scene. Ist it possible (and how?) to give the script a parameter which can be handled as variable inside the script to tell it which scene to switch to?
I got the first part easily. It checks some template to see if the current scene is not empty (means, light is off).
If so, it will activate a scene (“scene.turn_on”) whereas the target is defined by the “szene” variable (function?) later in the script.
If the light is off, it will do nothing. Stop.
variables:
ignore: []
name: Energie tanken
szene: "{%- set x = namespace(szenen=[]) %}\n{%- for hue in states.light\n|selectattr('attributes.is_hue_group',
'defined')\n|selectattr('state', 'eq', 'on')\n|map(attribute='object_id')\n|reject('in',
ignore)\n|list %}\n {%- set x.szenen = x.szenen + ['scene.' ~ hue ~ '_' ~ name|slugify]\n
\ |list %}\n{%- endfor %}\n{{ x.szenen }}"
The “scene” code block is confusing for me. There is some deep magic in the “szene” part. It appears it scrolls through all hue lights and filters for groups followed by a filter for “on” (btw: can we skip this as this is the condition from above?) and maps the ids to names for the following reject to exclude rooms (groups?).
Ok, so far it selects the hue lights (groups?) to be used in further steps.
Then it sets them. The “x” means the current selected value of the groups and set them to the given (“name”) scene.
Ok, I thing I got it.
Nevertheless I have some further suggesstions/ issues:
I Have a room where one light might be off and the other might be on. Appears the script does not cover this as the group itself is marked as “no scene” and therefore does not touch the single light which is on.
So I would like to change the part of attributes.is_hue_group to attributes.is_hue_lightif this is possible. Are these attribute selections documented somewhere?