Automation for Action on Sunrise

Yohoo!

I am trying to create an automation which changes my Hue light to scene “daylight” (cold and whiter light) when the sun rises.

Obviously this should only happen when the lamp itself is in any state except “off”.

Is there any change to create only a single automation which will check all configured Hue lamps if they are on (“not off”) and if so, switch scene?

Currently I have if configured the following way for every Hue lamp:

alias: OG Galerie Sonnenaufgang
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: 0
condition:
  - condition: device
    type: is_on
    device_id: 273ab14c391dabdf5a1650b95c2728a8
    entity_id: light.og_galerie
    domain: light
action:
  - service: scene.turn_on
    target:
      entity_id: scene.og_galerie_konzentrieren
    metadata: {}
mode: single

Thanks!

/KNEBB

I recently implemented something similar, inspired by user 123. This would be the script adapted for your case:


script:

  auto_an_szenen:
    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: []  ## Beispiel: 'kuche' —> object_id!
      name: Energie tanken ## Name der Hue-Szene
      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

You can use simply by calling the script or limit it:


- service: script.auto_an_szenen
  data:
    name: Energie tanken
    ignore:
      - wohnzimmer ## object_id!
      - kuche      ## object_id!

I use the object_id as input because I was too lazy to convert all the umlauts.

Hi,

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!

/KNEBB

Just like in your automation:


alias: OG Galerie Sonnenaufgang
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: 0
condition: []
action:
  - service: script.auto_an_szenen

mode: single

Hi,

thant’s indeed obvious.

My issue is where and how to create the script itself.

/KNEBB

Ouups…

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.

We are perfect in misunderstanding, are we?

I wrote it wrong, sorry for confusion.

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?

Thanks again and sorry for confusion.

/KNEBB

To be continued … :grin:

See my post No. 4

P.S.: You can directly call the script via service: script.xyz

I am really getting confused now…

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…

/KNEBB

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.

Still having no clue what you are referring to. But let’s have a look at the yaml-File itself:

root@hoas:/usr/share/hassio/homeassistant# cat scripts.yaml 
script:
  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=[]) %}\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 }}"
  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
root@hoas:/usr/share/hassio/homeassistant# 

We need the entity_id of the script.

How do I get it? It is not written in the yaml nor does the UI show it…

Go to Developer Tools —> Services and type in script. and look for the script.

Just to be sure: Did you create the new script via UI or otherwise?

1 Like

Well, now I got the idea my script is magically available in the services, indeed. Do not have an idea how this went… anyways.

Still do not get an ID:


Should I use “script.script” as ID?
Bildschirmfoto 2022-10-25 um 21.45.03

I do not remember how I created it. So far I tried both options and ended up with a version which is now valid…

/KNEBB

Funny name for a script :wink: Anyway… test your automation with script.script to see that it is working. Or you rename it first.

Yerah, tested it with script.script- however this name happened… automation now works. Some minor glitches but at least it executes the script now.

Thanks a lot, it is really not obvious how to use HomeAssistant sometimes…

Thanks again!

/KNEBB

Glad to hear!

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:)

1 Like

Perfect! Now it makes sense.

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?

Trying even deeper to understand the script. I will try to elaborate and learn. If you are in good mood you might explain :wink:

sequence:
  - if:
    - condition: template
      value_template: '{{ szene != [] }}'
    then:
    - service: scene.turn_on
      target:
        entity_id: '{{ szene }}'
    else:
    - stop: Lampen sind bereits eingeschaltet

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?

Anyways, thanks a lot, this really helped me!

/KNEBB