Running multiple services within a loop? (Philips Hue Activate scene in every room)

Hello!

I’m pretty new when it comes to Hassio scripting but I thought I’d try to create a ‘House mode’ script which would include triggering Philips Hue scene in every room depending on which scene is configured in a group of input_text entities. For now I’ve had them hardcoded but I wanted to see if I could do it in a loop.

This is the code I’ve created, which looks correct in the Template editor, I’m sorry in advance if the formatting is incorrect:

    - service_template: >-
    {% for entity_id in states.group.hue_home_config.attributes.entity_id %}
    {% set domain, device = entity_id .split('.') %}
    {% if states[domain][device].state != 'Off' %}
    - service: hue.hue_activate_scene
      data_template:
        group_name: {{states[domain][device].attributes.friendly_name .split(' Mode')[0]}}
        scene_name: {{states[domain][device].state}}
    {% else %}
    - service: light.turn_off
      data_template:
        entity_id: light.{{states[domain][device].attributes.friendly_name .split(' Mode')[0] | replace(" ","_") | lower}}
    {% endif %}
    {% endfor %}

the input_text files have the naming convention “Room name Mode Home” which is why I split with (’ Home’).

This is what the Template editor shows:

- service_template: >-
    - service: hue.hue_activate_scene
      data_template:
        group_name: Living room
        scene_name: Home
    - service: hue.hue_activate_scene
      data_template:
        group_name: Office
        scene_name: Home

Which is exactly the same as what I have hardcoded (except with it grabbing scene_name from the input_text entity, and without service_template)

And this is the error I’m getting:

Template rendered invalid service: - service: hue.hue_activate_scene
data_template:
group_name: Living room
scene_name: Home
- service: hue.hue_activate_scene
data_template:
group_name: Office
scene_name: Home

Is there anyone who understands what I would need to do to make this work?
I’ve tried removing “- service:” in the loop but I get the same error still.

Thank you!

The reason this is not working is because you’re trying to use Jinja to generate YAML code, but that won’t work. A Jinja template can only generate a single string. So what you’ve created is YAML code with just one parameter – namely service_template – that has a very large, multi-line string value, which is not a valid service name.

You can use loops in Jinja, but it has to be part of a template that generates just one YAML parameter value.

Loops, in general, in YAML are not easy. You’re better off writing a python_script.

1 Like

Thanks for your response! I’ll look into it.

edit: Got it working using the following code, incase anyone else is interested.

group_entities = hass.states.get('group.hue_'+data.get('mode')+'_config').attributes['entity_id']
for e in group_entities:
    group = (hass.states.get(e).attributes['friendly_name'].split(' Mode')[0])
    scene = (hass.states.get(e).state)
    room_entity = ("light." + group.replace(' ', '_').lower())
    if scene == 'Off':
        service_data = { 'entity_id': room_entity }
        hass.services.call('light', 'turn_off', service_data)
    elif scene == 'Keep':
        continue
    else:
        service_data = {'group_name': group, 'scene_name': scene}
        hass.services.call('hue', 'hue_activate_scene', service_data)

Calling it like so:

- service: python_script.hue_mode
  data_template:
    mode: 'home'

This is what ‘group.hue_home_config’ looks like

1 Like

Could you please help with hue configuration:

  1. How hue groups are created, in your group file, from individual bulbs?

  2. Should python script above be placed under folder /config/python_scripts ? Is the script section above complete?

  3. Do I still need any automation state trigger to change scene when your input_select drop down changes?

Sorry for asking so many questions, I have been trying first hue bulb working with input_select theme but nothing working. There are too many examples in forum using hue.hue_activate_scene and light.hue_activate_scene but for me nothing working.

I tried below code on a single hue bulb, picked up group “4” from hue developer’s page:

Input_select

hue_living_scenes:
    name: Select Hue Scenes
    options:
      - Arctic aurora
      - Bright
      - Concentrate
      - Cool white
      - Dimmed
      - Energize
      - Nightlight
      - Read
      - Relax
      - Savanna sunset
      - Spring blossom
      - Tropical twilight
    initial: Dimmed
    icon: mdi:creation #palette

Below is state change automation trigger:

Living Room Hue Scene Automation

- id: Change_Living_Room_Hue_Scene
  alias: Change Living Room Hue Scene
  trigger:
  - platform: state
    entity_id: input_select.hue_living_scenes
  action:
  - service: hue.hue_activate_scene
    data: 
      group_name: "4"
      data_template:
        entity_id: scene.{{ states.input_select.hue_living_scenes.state }}

No error message nothing works. Thanks for your help.

Is your group really called “4” and not something like “Living Room”?

Also the last part should be

action:
  - service: hue.hue_activate_scene
    data_template: 
      group_name: "4"
      scene_name: "scene.{{ states.input_select.hue_living_scenes.state }}" 

I was just going to point out the part about data_template. :slight_smile: But also, according to the docs, I think the second parameter should be scene_name, not entity_id.

Knew it still didn’t look right. Edited :+1:

I tried group_name “4” and “Living room” as visible from Philips hue api page, screen shot attached.

- id: Change_Living_Room_Hue_Scene
  alias: Change Living Room Hue Scene
  trigger:
  - platform: state
    entity_id: input_select.hue_living_scenes
  action:
  - service: hue.hue_activate_scene
    data: 
      group_name: "Living room"
      data_template:
        scene_name: "scene.{{ states.input_select.hue_living_scenes.state }}"

No error message and no action with hue light. Not sure what to do next!

You didn’t enter the action as @anon43302295 showed. Try that, it should work.

Not able to understand, which action to use? That should come out once the theme is selected in input_select. Below is full group “4” for “Living room” in hue api:

"4": {
  "name": "Living room",
  "lights": [
    "7"
  ],
  "sensors": [
    
  ],
  "type": "Room",
  "state": {
    "all_on": true,
    "any_on": true
  },
  "recycle": false,
  "class": "Living room",
  "action": {
    "on": true,
    "bri": 254,
    "hue": 44561,
    "sat": 32,
    "effect": "none",
    "xy": [
      0.3520,
      0.3450
    ],
    "ct": 208,
    "alert": "none",
    "colormode": "xy"
  }
},

As I said a few posts above (edited to include the group name, which is also what I said you’d probably need to do in the same post)

Thank you so much, the syntax for data_template was the last glitch, the format you advised worked perfectly.

1 Like