As always - template error

Can’t figure it out. Seems everything is in place but somehow just doesn’t work. Without the template with just light_on and light_off everything seems fine

- id: kabinet_zwave_s2_click
  alias: Kabinet light toggle button
  mode: single
  trigger:
  - platform: event
    event_type: zwave_js_value_notification
    event_data:
      node_id: 9
      value_raw: 26
  action:
  - service: light.turn_on
    entity_id: light.kabinet_left, light.kabinet_right
    data_template:  >-
          brightness: >
          {% if is_state("light.kabinet_left", "off") and is_state("light.kabinet_right", "off") %} 255
          {% elif is_state("light.kabinet_left", "off") and is_state("light.kabinet_right", "on") %} 255
          {% elif is_state("light.kabinet_left", "on") and is_state("light.kabinet_right", "on") %} 0
          {% endif %}

You’ll need to expand on what specifically isn’t working for me to help further.

But what jumps out at me is that you’re setting max brightness for when left and right lights are both off and also when left is off and right is on. But 0 brightness when both left and right are on.
It seems strange to me that you’re missing the case of left on and right off, and also that you’re setting 0 brightness when both lights are on. Are you sure the logic represents what you’re expecting?

Ok should go into more detail. Seems like with recent releases of HA setting the brightness to 0 switches off the lights. So if you have two or more light you want to control with a single button - you can skip the hassle of writing multiple light.toggle automations combine everything into just one simple one. And this is what i am trying to achieve. One z-wave button controls left and right light strips. So if both off - turn them on. If the right one is on (i have a separate scene for it) turn them on. If they are on - switch them off

Ok makes more sense although I think you’re missing the case of left off and right on (unless maybe that’s not feasible with a separate scene). So what specifically is not working. Or put another way, what is the present behavior?

Your example contains syntax errors:

  • There should not be a line-continuation character after data_template:
  • The template for brightness: isn’t indented sufficiently.

Here’s the corrected version that also employs the current way of specifying the targeted entities and desired options.

- id: kabinet_zwave_s2_click
  alias: Kabinet light toggle button
  mode: single
  trigger:
  - platform: event
    event_type: zwave_js_value_notification
    event_data:
      node_id: 9
      value_raw: 26
  action:
  - service: light.turn_on
    target:
      entity_id:
        - light.kabinet_left
        - light.kabinet_right
    data:
      brightness: >
        {% if is_state("light.kabinet_left", "off") and is_state("light.kabinet_right", "off") %} 255
        {% elif is_state("light.kabinet_left", "off") and is_state("light.kabinet_right", "on") %} 255
        {% elif is_state("light.kabinet_left", "on") and is_state("light.kabinet_right", "on") %} 0
        {% endif %}

NOTE

If you are interested, you can reduce the template to this:

      brightness: >
        {% set qty_on = expand('light.kabinet_left', 'light.kabinet_right') | selectattr('state', 'eq', 'on') | list | count %}
        {{ 255 if qty_on == 1 else 0 }}
1 Like

You nailed it.

  • There should not be a line-continuation character after data_template:

when did this happen? All my templates were like that before. And thank you for even shorter example

data_template was deprecated in favor of simply data many versions ago.

I’ve been using Home Assistant since 2018 and I don’t recall examples with a line-continuation character next to data_template. It’s news to me that it ever worked that way. :thinking: :man_shrugging: