Turn on light, if allready on, do something else

Hi guys.

Im starting to lose my hair over this.
I have a hue color bulb, and what i want is:
If the bulb is off, and i turn it on, it should be the normal white-yellow-ish light
If the bulb is allready on, and i hit the “turn on” button on my remote, it should go to the next color on the color wheel, and if i click and again, next color and so on.

The last part if have figured out, but not the first one.

This is what i go:

- alias: CycleColor
  initial_state: true
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: switch_4
      event: 1002
  action:
    - service: light.turn_on
      entity_id:
        - light.hue_bulb
      data_template: >-
        {%- if is_state('light.hue_bulb', 'on') -%}
        hs_color:
          - "{{ (30 + (state_attr('light.hue_bulb', 'hs_color')[0] or 0)) % 360 }}"
          - 100
        brightness_pct: 100
        {%- else -%}
        xy_color: [0.4497,0.408]
        color_temp: 330
        brightness: 254
        {%- endif -%}
        transition: 1

And the error i get:

2019-08-26 18:08:58 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected a dictionary for dictionary value @ data[‘action’][0][‘data_template’]. Got ‘{% if is_state('light.hue_bulb', 'on') %}\n hs_color:\n - “{{ (30 + (state_attr('light.hue_bulb', 'hs_color')[0] or 0)) % 360 }}”\n - 100\nbrightness_pct: 100 {% else %}\n xy_color: [0.4497,0.408]\n color_temp: 330\n brightness: 254\n{% endif %} transition: 1\n’. (See /config/automations.yaml, line 136). Please check the docs at Automation - Home Assistant

The xy color i got from just setting the color in HA and just seeing the event in the logs

A template cannot span multiple parameters. You need to use an individual template in each parameter.

Thanks for your quick response.

I have changed it up to something a bit more simple

     data_template:
        hs_color: >
          - {% if is_state('light.hue_bulb', 'on') %} {{ (30 + (state_attr('light.hue_bulb', 'hs_color')[0] or 0)) % 360 }} {% else %} 255 {% endif %}
          - 100
        brightness_pct: 100
        transition: 1

But that gives me:

Invalid data for call_service at pos 1: None for dictionary value @ data[‘hs_color’]

Also, by " You need to use an individual template in each parameter."
Do you mean it like i did? for each of my - i need an if?

Yes, that is better. I think the current issue with that snippet is you’re missing quotes. Try:

     data_template:
        hs_color: >
          - "{% if is_state('light.hue_bulb', 'on') %} {{ (30 + (state_attr('light.hue_bulb', 'hs_color')[0] or 0)) % 360 }} {% else %} 255 {% endif %}"
          - 100
        brightness_pct: 100
        transition: 1

And, yes, if you’re trying to calculate values for an array/list, you need to use a separate template for each value of the array/list. Which, BTW, would not be the case if my PR was accepted, but it has been ignored.

1 Like

Yea that would make sense, just hate doing the same if’s over and over again if i need to change multiple things on the same “block”

But the quotes around it didnt help, same error

Can you post the entire automation as it exist now (that is causing the error, and the error if it’s changed)?

Oh figured it out, had to remove the > from “hs_color: >”

Duh. Yes, should have noticed that. Good catch! :slight_smile:

Just a question from pure curiosity, this seems to work fine for me.
But what if i wanted to do as i first described.

only do hs_color if on, if not on then do xy_color?

That, unfortunately, is a bit problematic. HA’s script component is very basic. Probably the easiest way is to do the work in two different scripts, then use a service_template to call one or the other based on whether or not the light is currently on. Or maybe implement in a python_script.

Okay, i was afraid, you would say that :slight_smile:

Thanks alot for your help!

1 Like

Just wanted to second that thank you to pnbruckner. Kudos on such terrific, patient help on Terp’s issue. Well done.

1 Like