[solved] Help wanted: Cleanning up an ugly script for Hue control

Hi All,

I’m trying to change a rather ugly piece of code in a script into something nice.
Here is the original script:

  ## Added V1.2.0
  ## Script for setting Hue lamps in video or normal mode
  ## used in package_media_center
  hue_mode_change:
    alias: Licht aanpassen voor video of normale mode
    sequence:
      - service: script.set_hue_scene_auto
        data_template:
          brightness_stop_group_1: >
            {% if (states.input_select.livingroom_lights_mode.state == "Video") %}
              90
            {% else %}
              100
            {% endif %}
          brightness_stop_group_2: >
            {% if (states.input_select.livingroom_lights_mode.state == "Video") %}
              100
            {% else %}
              150
            {% endif %}
          profile_stop_1: >
            {% if (states.input_boolean.christmas_mode.state == "on") %}
              "christmas_normal_1"
            {% else %}
              "normal_1"
            {% endif %}
          profile_stop_2: >
            {% if (states.input_boolean.christmas_mode.state == "on") %}
              "christmas_normal_2"
            {% else %}
              "normal_2"
            {% endif %}
          transition: 5

Bassically I wnat to get rid of the double if statements
This is what I’d like to do after trying other variants as well:

  ## Added V1.2.0
  ## Script for setting Hue lamps in video or normal mode
  ## used in package_media_center
  hue_mode_change:
    alias: Licht aanpassen voor video of normale mode
    sequence:
      - service: script.set_hue_scene_auto
        data_template:
          brightness: >
            {% if (states.input_select.livingroom_lights_mode.state == "Video") %}
              {% set brightness = {"one": 90, "two": 100} %}
              {{ brightness }}
            {% else %}
              {% set brightness = {"one": 100, "two": 150} %}
              {{ brightness }}
            {% endif %}
          profile: >
            {% if (states.input_boolean.christmas_mode.state == "on") %}
              {% set profile = {"one": "christmas_normal_1", "two": "christmas_normal_2"} %}
              {{ profile }}
            {% else %}
              {% set profile = {"one": "normal_1", "two": "normal_2"} %}
              {{ profile }}
            {% endif %}
          transition: 5

With the last example code the next script should do this:

  ## Script for setting Hue lamps into a new mode (from off)
  ## 
  set_hue_scene_auto:
    alias: Set hue mode overgang naar andere scene
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: light.lage_kasten_hue
          # brightness: "{{ brightness_stop_group_1 }}"
          # profile: "{{ profile_stop_1 }}"
          brightness: "{{ brightness.one|int }}"
          profile: "{{ profile.one }}"
          transition: "{{ transition|float }}"
      - service: light.turn_on
        data_template:
          entity_id: light.staande_lampen_hue
          # brightness: "{{ brightness_stop_group_2 }}"
          # profile: "{{ profile_stop_2 }}"
          brightness: "{{ brightness.two|int }}"
          profile:  "{{ profile.two }}"
          transition: "{{ transition|float }}"

I left in the original code lines (commented) that were in the code that worked with the original script.

Who can help me out here. The code is not reporting errors (on config check) but is not working.

Thanks all,

Ralph

Items passed to scripts are strings, not objects. Therefore, just use simple strings and parse the data.

  ## Added V1.2.0
  ## Script for setting Hue lamps in video or normal mode
  ## used in package_media_center
  hue_mode_change:
    alias: Licht aanpassen voor video of normale mode
    sequence:
      - service: script.set_hue_scene_auto
        data_template:
          brightness: >
            {{ '90,100' if is_state('input_select.livingroom_lights_mode','Video') else '100,150' }}
          profile: >
            {{ 'christmas_normal' if is_state('input_select.christmas_mode','on') else 'normal' }}
          transition: 5
  ## Script for setting Hue lamps into a new mode (from off)
  ## 
  set_hue_scene_auto:
    alias: Set hue mode overgang naar andere scene
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: light.lage_kasten_hue
          brightness: "{{ brightness.split(',')[0] | int }}"
          profile: "{{ '{}_1'.format(profile) }}"
          transition: "{{ transition | float }}"
      - service: light.turn_on
        data_template:
          entity_id: light.staande_lampen_hue
          brightness: "{{ brightness.split(',')[1] | int }}"
          profile:  "{{ '{}_2'.format(profile) }}"
          transition: "{{ transition | float }}"

@petro: Thanks for this response. Looks logical and promising. I was already afraid that objects ended up being the nog-go area. I will try your suggestions, but this will most likely be after Christmas as tinkering with HA is definitely not within the WAF margins :slight_smile: .

Ralph

1 Like

So Christmas is over and after taking a day to do some other stuff I tried your suggestions. These did not work out at first, however after some trying I came up with this working solution.
script one:

  hue_mode_change:
    alias: Licht aanpassen voor video of normale mode
    sequence:
      - service: script.set_hue_scene_auto
        data_template:
          brightness_value: >
            {{ '90,100' if is_state('input_select.livingroom_lights_mode','Video') else '100,150' }}
          profile_value: >
            {{ 'christmas' if is_state('input_boolean.christmas_mode','on') else 'normal' }}
          transition: 5

and the second script:

  set_hue_scene_auto:
    alias: Set hue mode overgang naar andere scene
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: light.lage_kasten_hue
          brightness: >
            {{ brightness_value.split(',')[0]|int }}
          profile: >
            {{ '{}_1'.format(profile_value) }}
          transition: "{{ transition|float }}"
      - service: light.turn_on
        data_template:
          entity_id: light.staande_lampen_hue
          brightness: >
            {{ brightness_value.split(',')[1]|int }}
          profile: >
            {{ '{}_2'.format(profile_value) }}
          transition: "{{ transition|float }}"

As you can see I had to change the way brightness and profile where used in the second script. Two things happened:

  • I had to change the names into brightness_value and profile_value. without the _value part, the variables are not accepted by HA (maybe because they are similar to the parameter name?)
  • I had to use the notation with brightness: > as the suggested way did not work. Even though |int was used, HA reported an error saying that it had to be an integer. Keeping it at the same line without the quotes resulted in an error already at configuration check.
    Anyway thanks for the help @petro. Now I can go to next step and update all the automations that use (mainly) the second script.

Regards,

Ralph