Use list in automation (data_template)

Hi everyone

I have an enOcean switch for which I set a different target (light) to be toggled in case the respective button is pressed. For one button I would like to toggle two lights. Unfortunately, I apparently did not correctly use a list in my template, as the below code only works for the single lights:

service: light.toggle
data_template:
  entity_id: |
    {% if trigger.event.data.which == 1 %}
      {% if trigger.event.data.onoff == 0 %}
        light.buro_fernsehlampe
      {% elif trigger.event.data.onoff == 1 %}
        light.zigfred_wohnzimmer_lampen
      {% endif %}
    {% elif trigger.event.data.which == 0 %}
      {% if trigger.event.data.onoff == 0 %}
        light.zigfred_wohnzimmer_dimmer
      {% elif trigger.event.data.onoff == 1 %}
        - light.buro_tisch_links
        - light.buro_tisch_rechts
      {% endif %}
    {% endif %}

How should I phrase the last bit so that a correct list is returned and both lights (light.buro_tisch_links and light.buro_tisch_rechts) are toggled?
Thank you for any hint.

Try this

      {% elif trigger.event.data.onoff == 1 %}
        light.buro_tisch_links, light.buro_tisch_rechts
      {% endif %}
1 Like

You can output actual list objects now. So this should work:

service: light.toggle
data:
  entity_id: |
    {% if trigger.event.data.which == 1 %}
      {% if trigger.event.data.onoff == 0 %}
        light.buro_fernsehlampe
      {% elif trigger.event.data.onoff == 1 %}
        light.zigfred_wohnzimmer_lampen
      {% endif %}
    {% elif trigger.event.data.which == 0 %}
      {% if trigger.event.data.onoff == 0 %}
        light.zigfred_wohnzimmer_dimmer
      {% elif trigger.event.data.onoff == 1 %}
        {{ ['light.buro_tisch_links', 'light.buro_tisch_rechts'] }}
      {% endif %}
    {% endif %}

Also I replaced data_template with data. You haven’t needed to use data_template to use templates in service calls since WTH 2020.

2 Likes
service: light.toggle
target:
  entity_id: |
    {% set lights = ['light.zigfred_wohnzimmer_dimmer', ['light.buro_tisch_links', 'light.buro_tisch_rechts'],
      'light.buro_fernsehlampe', 'light.zigfred_wohnzimmer_lampen'] %}
    {% set i = trigger.event.data.which * 2 + trigger.event.data.onoff %}
    {{ lights[i] }}
2 Likes

Thank you all for your help. I went for CentralCommand’s version that works perfectly.

Just to confirm, did what I suggest work or did it fail?

Have not tested it before. But now I did and can confirm it works. In my view your solution is very smart but not as simple to understand and/or maintain (in case an entity name changes or so).

It’s fairly straightforward. You just need to know a little bit about lists and binary numbers. You’ll see it takes more words to explain the technique than it does code to implement it. :slightly_smiling_face:

Lists contains items. In this case, the items are the lights.

Lists are indexed starting with zero. That means in a list containing four items, the first item’s index is zero and the last one is three.

which and onoff are either 0 or 1 which effectively makes them binary numbers.

The combination of which and onoff forms a binary number that can range from decimal 0 to 3.

Decimal Value which onoff
0 0 0
1 0 1
2 1 0
3 1 1

Knowing these basic concepts, we can put the lights in a list and then simply select them by their index.

Here’s the list of 4 lights. Remember, the first one’s index is 0 and the last one is 3.

{% set lights = [
                'light.zigfred_wohnzimmer_dimmer',
                ['light.buro_tisch_links', 'light.buro_tisch_rechts'],
                'light.buro_fernsehlampe',
                'light.zigfred_wohnzimmer_lampen'
                ] %}

If you every need to change an entity_id, just change it in the list. Easy-peasy.

This line simply converts the combined binary values of which and onoff into a decimal value. This value is used as the list index.

    {% set i = trigger.event.data.which * 2 + trigger.event.data.onoff %}

The last line simply gets an item from the list based on the index value. In other words, it gets the light corresponding to the combination of which and onoff.

{{ lights[i] }}

The nice thing about this technique is it’s easily expanded to handle more than just 4 combinations. In contrast, expanding an if elif chain to support 8 combinations would produce many repetitive lines of code.

It’s also easy to maintain because all of the data (the names of the lights) is all in one place (in the list).