Automation with data_template setting multiple values

Hi, I would like to set the brightness and colour of my light depending on time of day.
Since both values require the same if then else statement I was hoping I could do it in 1. Is that possible?

This works:

- alias: Ensuite Light by motion ON
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00016570dc
      to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: light.ensuite_light
    data_template: 
      brightness: >
        {% if states.sensor.time_of_day.state == "night"  %} 80
        {% elif states.sensor.time_of_day.state == "late evening" %} 150
        {% else %} 255
        {% endif %}
      color_name: >
        {% if states.sensor.time_of_day.state == "night"  %} green
        {% elif states.sensor.time_of_day.state == "late evening" %} blue
        {% else %} white
        {% endif %}

but I would like to have something like this:

- alias: Ensuite Light by motion ON
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00016570dc
      to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: light.ensuite_light
    data_template: 
      >
      {% if states.sensor.time_of_day.state == "night"  %} 
      brightness: 80
      color_name: blue        
      {% elif states.sensor.time_of_day.state == "late evening" %}
      brightness: 150
      color_name: green      
      {% else %}
      brightness: 255
      color_name: white            
      {% endif %}

in the dev console it looks OK, but there are no examples that would support that this should work.
Is it possible?

AFAIK you have to do it the way you have it in the first one because the template spits out a string, not formatted keys (for want of a better way to explain it).

you could also create a scene per desired situation. scene.night, scene.late_evening, scene.default.

Which I always find easier to maintain, since you’d not need to change the automation, but only the scene whenever you want to change the end-effect. Keeping the logic and the content apart. Safe and simple.

this is what I use, nothing special.
Though when hassio updates start to fall apart and stop supporting rgb color settings like this, which seems to have happened reading all comments after 66 came along, it is easier to change only the scene, and not have to dive into the automation…

- name: Opstart
  entities:
    light.kist:
      state: on
      transition: 4
      brightness: 199
#      color_temp: 153
      rgb_color: [255,93,42]

- name: Naar bed
  entities:
    light.bedside_table:
      state: on
      transition: 4
      brightness: 37
#      color_temp: 153
      rgb_color: [171,58,22]

- name: Opstaan
  entities:
    light.kist:
      state: on
      transition: 4
      brightness: 199
#      color_temp: 153
      rgb_color: [255,93,42]

- name: Gym
  entities:
    light.kist:
      state: on
      transition: 4
      brightness: 199
#      color_temp: 153
      rgb_color: [255,93,42]

- name: Selamat makan
  entities:
    light.dining_table_lamp_1:
      state: on
      transition: 4
      brightness: 206
      color_temp: 443
    light.dining_table_lamp_2:
      state: on
      transition: 4
      brightness: 206
      color_temp: 443  
    light.dining_table_lamp_3:
      state: on
      transition: 4
      brightness: 206
      color_temp: 443

- name: Aan de slag
  entities:
    light.bureau:
      state: on
      transition: 4
      brightness: 233
      color_temp: 370
    light.inside:
      state: on
      transition: 4
      brightness: 127
#      color_temp: 370
      rgb_color: [218,176,100]
    light.outside:
      state: on
      transition: 4
      brightness: 127
#      color_temp: 397
      rgb_color: [222,174,91]

- name: Uit huis
  entities:
    light.kist:
      state: on
      transition: 4
      brightness: 199
#      color_temp: 153
      rgb_color: [255,93,42]
1 Like

Try using a dictionary. It may work. I haven’t tried it before:

  action:
    service: homeassistant.turn_on
    entity_id: light.ensuite_light
    data_template: >
      {% set mydict = {'brightness':255, 'color_name':'white'} %}
      {% if states.sensor.time_of_day.state == "night"  %} 
      {% set mydict['brightness'] = 80 %}
      {% set mydict['color_name'] = 'blue' %}     
      {% elif states.sensor.time_of_day.state == "late evening" %}
      {% set mydict['brightness'] = 150 %}
      {% set mydict['color_name'] = 'green' %}
      {% endif %}
      {{ mydict }}

A dictionary has the same shape as json objects. If this doesn’t work, you may need to change the last line to:

{{ {'data':mydict} }}

Give it a whirl and let me know the outcome

3 Likes

Thanks for that idea, unfortunately that did not work either. (I tried both options)

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None.

That sounds like to proper solution.
But that would mean 1 scene per time-period per room?
It looks like scenes do not support data-templates (yet)?

I will need to think about this option. It is a bit of a change from the current setup.

Also this statement in the documentation means I then need to maintain the list of entities in the scene also in the automation to turn all entities off.

Scenes can be activated using the service scene.turn_on (there is no ‘scene.turn_off’ service).

But thanks for the input. I will investigate further.

What @Mariusthvdb is saying is that you use a script/scene to pass variables to. But I think you’ll have the same issue that we have above. I think a good enhancement to hass would be to have the template parser handle lists and dictionaries. It wouldn’t be too hard to implement IMO.

Anyways, for now it looks like you’ll need 2x the logic or a script for each ‘if branch’.