Control hue scene by script

I am building a script to control som philips hue lights.

I need to change the schene.

I have a input select:

input_select:
  spisebord_room_preset:
    options:
      - Relax
      - Read
      - Concentrate
      - Energize

My question is how do i use these options to control the lights:

when hardcoding it works:

- alias: Set spisebord_room_preset
  trigger:
    platform: state
    entity_id: input_select.spisebord_room_preset
  action:
    service: light.hue_activate_scene
    data:
      group_name: 'Spisebord'
      scene_name: 'Energize'

but not when scripting:

- alias: Set spisebord_room_preset
  trigger:
    platform: state
    entity_id: input_select.spisebord_room_preset
  action:
    service: light.hue_activate_scene
    data:
      group_name: 'Spisebord'
      scene_name: '{{ states.input_select.spisebord_room_preset.state }}'

I have tryed all sorts of combinations with no luck:

scene_name: {{ states.input_select.spisebord_room_preset.state }}
scene_name: "{{ states.input_select.spisebord_room_preset.state }}"
scene_name: "{{ states.input_select.spisebord_room_preset}}"

Not sure, but i think you should use data_template in your action.

Maybe it works with something like this:

Create an automation like this:

- alias: Set spisebord_room_preset
  trigger:
    platform: state
    entity_id: input_select.spisebord_room_preset
  action:
    service: script.turn_on
    entity_id: script.input_select_spisebord_script

And this in your scripts.yaml or under scripts section in your configuration.yaml:

- input_select_spisebord_script:
  alias: "Set spisebord_room_preset"
  sequence:
      service: light.hue_activate_scene
      data_template:
        group_name: 'Spisebord'
        scene_name: >
          {% if is_state("input_select.spisebord_room_preset", "Relax") %}
              'Relax'
          {% elif is_state("input_select.spisebord_room_preset", "Read") %}
              'Read'
          {% elif is_state("input_select.spisebord_room_preset", "Concentrate") %}
              'Concentrate'
          {% elif is_state("input_select.spisebord_room_preset", "Energize") %}
              'Energize'
          {% endif %}

Good luck!

Hi all

Thank you for your replies, it lead me very much in the right way.

Below is the working solution

- alias: Set spisebord_room_preset
  trigger:
    platform: state
    entity_id: input_select.spisebord_room_preset
  action:
    service: light.hue_activate_scene
    data_template:
        group_name: 'Spisebord'
        scene_name: >
          {% if is_state("input_select.spisebord_room_preset", "Relax") %}
              Relax
          {% elif is_state("input_select.spisebord_room_preset", "Read") %}
              Read
          {% elif is_state("input_select.spisebord_room_preset", "Concentrate") %}
              Concentrate
          {% elif is_state("input_select.spisebord_room_preset", "Energize") %}
              Energize
          {% endif %}
2 Likes

Maybe it could be a little shorter with:

action:
  service: light.hue_activate_scene
  data_template:
    group_name: 'Spisebord'
    scene_name: '{{ trigger.to_state.state }}'

Just an attempt.

4 Likes

Works perfectly

Thank you very much

This is exactly what I needed. Thanks!

I also want to turn off the lights with the same input panel. Can anyone tell me how to achieve this? I tried running a script within the if function in automation but that didnt do the job…