If statements in service call (for color wheel)

Hi,

I would like to toggle through light colors by adjusting the hs_color value, which I want to trigger with a remote.

Therefore I try to increment the hue value of the hs_color attribute of the light. Since values over 360 seems not be permitted, I want to catch the exception using an if statement.

Here is my code:

service: light.turn_on
data:
  hs_color:
                {%- if state_attr(states('input_select.selectedlight'), 'hs_color')  %}
                  {%- if state_attr(states('input_select.selectedlight'), 'hs_color')[0] + 18 > 360 %}
                    {%- set hue = (state_attr(states('input_select.selectedlight'), 'hs_color')[0] + 18 - 360) %}
                    {%- set sat = (state_attr(states('input_select.selectedlight'), 'hs_color')[1] ) %}
                  {%- else %} 
                    {%- set hue = (state_attr(states('input_select.selectedlight'), 'hs_color')[0] + 18) %}
                    {%- set sat = (state_attr(states('input_select.selectedlight'), 'hs_color')[1] )  %}
                  {%- endif %}
                {%- else %}
                  {%- set hue = 10 %}
                  {%- set sat = 100 %}
                {%- endif %} 
               - {{hue}}
               - {{sat}}
target:
  entity_id: '{{ states.input_select.selectedlight.state }}'

This results in the following code in the template editor, which works if I copy & paste it into the service call:

service: light.turn_on
data:
  hs_color: 
               - 242.916
               - 70.196
target:
  entity_id: 'light.lampe_couch_level_light_color_on_off'

Unfortunately, the dynamic code with the if statements doesn’t work in the service call and after a few hours I still haven’t figured out why.

Could somebody please advise?

Thanks
Steffen

Replace this:

               - {{hue}}
               - {{sat}}

with this:

               [ {{hue}}, {{sat}} ]
1 Like

Thanks Taras, but it’s still the same… It works when manually using the output from the template editor, but not when the if statements are present. I’ve tested it with hardcoded values in your suggested format [123, 45]:

Nothing happens:

service: light.turn_on
data:
  hs_color:
                {%- if state_attr(states('input_select.selectedlight'), 'hs_color')  %}
                  {%- if state_attr(states('input_select.selectedlight'), 'hs_color')[0] + 18 > 360 %}
                    {%- set hue = (state_attr(states('input_select.selectedlight'), 'hs_color')[0] + 18 - 360) %}
                    {%- set sat = (state_attr(states('input_select.selectedlight'), 'hs_color')[1] ) %}
                  {%- else %} 
                    {%- set hue = (state_attr(states('input_select.selectedlight'), 'hs_color')[0] + 18) %}
                    {%- set sat = (state_attr(states('input_select.selectedlight'), 'hs_color')[1] )  %}
                  {%- endif %}
                {%- else %}
                  {%- set hue = 10 %}
                  {%- set sat = 100 %}
                {%- endif %} 
               [ 123, 45 ]
target:
  entity_id: '{{ states.input_select.selectedlight.state }}'

When all if-statements are removed it works:

service: light.turn_on
data:
  hs_color:
               [ 123, 45 ]
target:
  entity_id: '{{ states.input_select.selectedlight.state }}'

You need to put a line-continuation character, either > or |, after hs_color: otherwise it’s invalid.

data:
  hs_color: >

Try this version:

service: light.turn_on
target:
  entity_id: "{{ states('input_select.selectedlight') }}"
data:
  hs_color: >
    {% set hs = state_attr(states('input_select.selectedlight'), 'hs_color') %}
    {% if hs is iterable %}
      {% set (hue, sat) = hs %}
      {% set hue = hue + 18 - (360 if hue + 18 > 360 else 0) %}
    {% else %}
      {% set (hue, sat) = (10, 100) %}
    {% endif %} 
    [ {{ hue }}, {{ sat }} ]
1 Like

This works. Thank you very much!

Do you have any tipps for better understanding the syntax?

P.S.: I’ve just noticed, that you have also optimized the rest of the code, which is brilliant!

1 Like