Home Assistant: having trouble setting switch color using a variable

ZHA Zigbee Inovelli Blues.

I am attempt to have it so that when my home battery is rapidly charging off the grid, the switch changes from red (0-15% SoC) up to blue, at 100% SoC, while pulsing (when the Alarm is off, as the light switches indicate alarm status when it’s on).

alias: Tesla Rapid Grid Charging Indicator Lights
description: Indicator lights for Tesla charging from the grid
trigger:
  - platform: numeric_state
    entity_id: sensor.grid_power
    above: 14500
condition:
  - condition: state
    entity_id: binary_sensor.battery_charging
    state: "on"
  - condition: state
    entity_id: alarm_control_panel.alarmo
    state: disarmed
action:
  - variables:
      battery_level: "{{ states('sensor.battery') | int }}"
      color: >
        {% if battery_level <= 15 %}
          1
        {% elif battery_level <= 100 %}
          {{ (1 + (battery_level - 15) * 2) | int }}
        {% else %}
          170
        {% endif %}
  - type: issue_all_led_effect
    domain: zha
    device_id: 0653d29eb9407d682856d13d0208937c
    effect_type: Pulse
    color: "{{ color }}"
    level: 100
    duration: 255
mode: single

I’ve also tried making the color: "{{ color | int }}

Either way I get the error:

Message malformed: expected int for dictionary value @ data[‘color’]

What am I missing here?

Try changing the variable name to something other than color. I think it’s confusing the variable and color in the k/v pair.

Interesting idea, but sadly no improvement.

Remove quotes in battery assignment. This is a prototype I ran in the developer tools

{%set battery_level=states('sensor.dehumid_leak_battery') | int %}
{% if battery_level <= 15 %}
  1
{% elif battery_level <= 100 %}
  {{ (1 + (battery_level - 15) * 2) | int }}
{% else %}
  170
{% endif %}

New error: “Message malformed: expected int for dictionary value @ data[‘color’]”

Just to confirm I did what you’re saying, here’s my current version:

alias: Tesla Rapid Grid Charging Indicator Lights 2
description: Indicator lights for Tesla charging from the grid
trigger:
  - platform: numeric_state
    entity_id: sensor.grid_power
    above: 14500
condition:
  - condition: state
    entity_id: binary_sensor.battery_charging
    state: "on"
  - condition: state
    entity_id: alarm_control_panel.alarmo
    state: disarmed
action:
  - variables:
      {%set battery_level=states('sensor.battery') | int %}
      color: >
        {% if battery_level <= 15 %}
          1
        {% elif battery_level <= 100 %}
          {{ (1 + (battery_level - 15) * 2) | int }}
        {% else %}
          170
        {% endif %}
  - type: issue_all_led_effect
    domain: zha
    device_id: 0653d29eb9407d682856d13d0208937c
    effect_type: Pulse
    color: "{{ color }}"
    level: 100
    duration: 255
mode: single

In case anyone read that before I edited it, it’s actually a new/different error now than before about the color value.

Try removing the quotes around {{ color }}. The line above level: 100

“Message malformed: Integration ‘’ not found”

Next version to try. I did my best to replicate with a different service call.

action:
  - variables:
      battery_level=states('sensor.battery') | int
      color: |
        {% if battery_level <= 15 %}
          1
        {% elif battery_level <= 100 %}
          {{ (1 + (battery_level - 15) * 2) | int }}
        {% else %}
          170
        {% endif %}
  - type: issue_all_led_effect
    domain: zha
    device_id: 0653d29eb9407d682856d13d0208937c
    effect_type: Pulse
    color: '{{ states("var.color") }}'
    level: 100
    duration: 255
mode: single

Same error, sadly. “Message malformed: Integration ‘’ not found”

Just to make sure we’re still on the same page:

description: Indicator lights for Tesla charging from the grid 2
mode: single
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.grid_power
    above: 11000
condition:
  - condition: state
    entity_id: binary_sensor.battery_charging
    state: "on"
  - condition: state
    entity_id: alarm_control_panel.alarmo
    state: disarmed
action:
  - variables:
      battery_level=states('sensor.battery') | int
      color: |
        {% if battery_level <= 15 %}
          1
        {% elif battery_level <= 100 %}
          {{ (1 + (battery_level - 15) * 2) | int }}
        {% else %}
          170
        {% endif %}
  - type: issue_all_led_effect
    domain: zha
    device_id: 0653d29eb9407d682856d13d0208937c
    effect_type: Pulse
    color: '{{ states("var.color") }}'
    level: 100
    duration: 255
mode: single

Ah ha. Try changing the battery_level=states('sensor.battery') | int to battery_level: states('sensor.battery') | int

description: Indicator lights for Tesla charging from the grid 2
mode: single
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.grid_power
    above: 11000
condition:
  - condition: state
    entity_id: binary_sensor.battery_charging
    state: "on"
  - condition: state
    entity_id: alarm_control_panel.alarmo
    state: disarmed
action:
  - variables:
      battery_level: states('sensor.battery') | int
      color: |
        {% if battery_level <= 15 %}
          1
        {% elif battery_level <= 100 %}
          {{ (1 + (battery_level - 15) * 2) | int }}
        {% else %}
          170
        {% endif %}
  - type: issue_all_led_effect
    domain: zha
    device_id: 0653d29eb9407d682856d13d0208937c
    effect_type: Pulse
    color: '{{ states("var.color") }}'
    level: 100
    duration: 255
mode: single

EDIT: I misunderstood the issue.

Ah ha. Try changing the battery_level=states('sensor.battery') | int to battery_level: states('sensor.battery') | int

“Message malformed: Integration ‘’ not found”

I don’t think the issue has anything to do with the variables, because I have a working automation that uses them as they are (when the grid is offline, it makes the color of the switches indicate battery level in the same way-- but no pulsing.

As in, this works-- there’s just no way (that I know of) to add pulsing to it:

alias: Light Switches Display Battery Level when Grid Offline
description: >
  Changes status light colors based on Tesla home battery level when the grid is
  offline.
trigger:
  - platform: state
    entity_id: binary_sensor.grid_status_2
    to: "off"
  - platform: state
    entity_id: sensor.battery
condition:
  - condition: state
    entity_id: binary_sensor.grid_status_2
    state: "off"
action:
  - service: number.set_value
    target:
      entity_id:
        - number.inovelli_vzm31_sn_default_all_led_off_color
        - number.light_switch_default_all_led_on_color_2
        - number.inovelli_vzm31_sn_default_all_led_off_color_2
        - number.inovelli_vzm31_sn_default_all_led_on_color_2
        - number.inovelli_vzm31_sn_default_all_led_off_color_4
        - number.inovelli_vzm31_sn_default_all_led_on_color_4
        - number.light_switch_default_all_led_off_color_2
        - number.light_switch_default_all_led_on_color
        - number.light_switch_default_all_led_off_color
        - number.inovelli_vzm31_sn_default_all_led_on_color
        - number.inovelli_vzm31_sn_default_all_led_off_color_3
        - number.inovelli_vzm31_sn_default_all_led_on_color_3
        - number.light_switch_default_all_led_off_color_4
        - number.light_switch_default_all_led_on_color_4
        - number.light_switch_default_all_led_off_color_5
        - number.light_switch_default_all_led_on_color_5
        - number.light_seitch_default_all_led_off_color_5
        - number.light_seitch_default_all_led_on_color_5
        - number.inovelli_vzm31_sn_default_all_led_off_color_5
        - number.inovelli_vzm31_sn_default_all_led_on_color_5
        - number.inovelli_vzm31_sn_default_all_led_off_color_6
        - number.inovelli_vzm31_sn_default_all_led_on_color_6
        - number.light_switch_room_default_all_led_off_color_5
        - number.light_switch_room_default_all_led_on_color_5
        - number.light_switch_shower_default_all_led_off_color_5
        - number.light_switch_shower_default_all_led_on_color_5
    data:
      value: >
        {% set battery_level = states('sensor.battery') |
        int %} {% if battery_level <= 15 %}
          1
        {% elif battery_level <= 100 %}
          {{ 1 + (battery_level - 15) * 2 }}
        {% else %}
          170
        {% endif %}
mode: single

Similarly, this also works (what I’m currently using for the purpose of this thread)-- it’s just limited to 7 color steps to indicate charge level, instead of 100. But, this working implies my trigger and conditions are fine-- it’s just the setting of the color/pulsing that is snagging it.

alias: Tesla Rapid Grid Charging Indicator Lights
description: Indicator lights for Tesla charging from the grid
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.grid_power
    above: 11000
condition:
  - condition: state
    entity_id: binary_sensor.battery_charging
    state: "on"
  - condition: state
    entity_id: alarm_control_panel.alarmo
    state: disarmed
action:
  - if:
      - condition: numeric_state
        entity_id: sensor.battery
        below: 15
    then:
      - service: script.inovelli_blue_vzm31_sn_animated_notifications_zha
        metadata: {}
        data:
          led: All
          color: Red
          level: 100
          effect: Pulse
          duration: Indefinitely
          target:
            device_id:
              - c8face0983b5ab8e6ca7935bcce579bf
              - 0653d29eb9407d682856d13d0208937c
              - 62e5da15a2b75c09dbca0d9cc73e6a71
              - 4d328b3aa26b9354955a6450c590e101
              - cef2485fe7542cafdbd63d96adeae393
              - 3abdf970845325f6e9970e24bbf5b83e
              - 19e870bc60c4eb263fcd0e42e721a535
              - 4bf83af2c19f1b57a0b97c16c93785eb
              - 8b0e72479260324334e13d3c56249eb9
              - e7f14e3d27bae40492ee67be4931910f
              - 1104b44193b967c6a85ab6ef12632f0e
              - e70b56fd2df88cebcdb44890716e824d
              - 1772c7a954c0ee15b4621ff990e71ae8
    else:
      - if:
          - condition: numeric_state
            entity_id: sensor.battery
            below: 30
        then:
          - service: script.inovelli_blue_vzm31_sn_animated_notifications_zha
            metadata: {}
            data:
              led: All
              color: Orange
              level: 100
              effect: Pulse
              duration: Indefinitely
              target:
                device_id:
                  - c8face0983b5ab8e6ca7935bcce579bf
                  - 0653d29eb9407d682856d13d0208937c
                  - 62e5da15a2b75c09dbca0d9cc73e6a71
                  - 4d328b3aa26b9354955a6450c590e101
                  - cef2485fe7542cafdbd63d96adeae393
                  - 3abdf970845325f6e9970e24bbf5b83e
                  - 19e870bc60c4eb263fcd0e42e721a535
                  - 4bf83af2c19f1b57a0b97c16c93785eb
                  - 8b0e72479260324334e13d3c56249eb9
                  - e7f14e3d27bae40492ee67be4931910f
                  - 1104b44193b967c6a85ab6ef12632f0e
                  - e70b56fd2df88cebcdb44890716e824d
                  - 1772c7a954c0ee15b4621ff990e71ae8
        else:
          - if:
              - condition: numeric_state
                entity_id: sensor.battery
                below: 45
            then:
              - service: script.inovelli_blue_vzm31_sn_animated_notifications_zha
                metadata: {}
                data:
                  led: All
                  color: Yellow
                  level: 100
                  effect: Pulse
                  duration: Indefinitely
                  target:
                    device_id:
                      - c8face0983b5ab8e6ca7935bcce579bf
                      - 0653d29eb9407d682856d13d0208937c
                      - 62e5da15a2b75c09dbca0d9cc73e6a71
                      - 4d328b3aa26b9354955a6450c590e101
                      - cef2485fe7542cafdbd63d96adeae393
                      - 3abdf970845325f6e9970e24bbf5b83e
                      - 19e870bc60c4eb263fcd0e42e721a535
                      - 4bf83af2c19f1b57a0b97c16c93785eb
                      - 8b0e72479260324334e13d3c56249eb9
                      - e7f14e3d27bae40492ee67be4931910f
                      - 1104b44193b967c6a85ab6ef12632f0e
                      - e70b56fd2df88cebcdb44890716e824d
                      - 1772c7a954c0ee15b4621ff990e71ae8
            else:
              - if:
                  - condition: numeric_state
                    entity_id: sensor.battery
                    below: 60
                then:
                  - service: script.inovelli_blue_vzm31_sn_animated_notifications_zha
                    metadata: {}
                    data:
                      led: All
                      color: Green
                      level: 100
                      effect: Pulse
                      duration: Indefinitely
                      target:
                        device_id:
                          - c8face0983b5ab8e6ca7935bcce579bf
                          - 0653d29eb9407d682856d13d0208937c
                          - 62e5da15a2b75c09dbca0d9cc73e6a71
                          - 4d328b3aa26b9354955a6450c590e101
                          - cef2485fe7542cafdbd63d96adeae393
                          - 3abdf970845325f6e9970e24bbf5b83e
                          - 19e870bc60c4eb263fcd0e42e721a535
                          - 4bf83af2c19f1b57a0b97c16c93785eb
                          - 8b0e72479260324334e13d3c56249eb9
                          - e7f14e3d27bae40492ee67be4931910f
                          - 1104b44193b967c6a85ab6ef12632f0e
                          - e70b56fd2df88cebcdb44890716e824d
                          - 1772c7a954c0ee15b4621ff990e71ae8
                else:
                  - if:
                      - condition: numeric_state
                        entity_id: sensor.battery
                        below: 75
                    then:
                      - service: >-
                          script.inovelli_blue_vzm31_sn_animated_notifications_zha
                        metadata: {}
                        data:
                          led: All
                          color: Cyan
                          level: 100
                          effect: Pulse
                          duration: Indefinitely
                          target:
                            device_id:
                              - c8face0983b5ab8e6ca7935bcce579bf
                              - 0653d29eb9407d682856d13d0208937c
                              - 62e5da15a2b75c09dbca0d9cc73e6a71
                              - 4d328b3aa26b9354955a6450c590e101
                              - cef2485fe7542cafdbd63d96adeae393
                              - 3abdf970845325f6e9970e24bbf5b83e
                              - 19e870bc60c4eb263fcd0e42e721a535
                              - 4bf83af2c19f1b57a0b97c16c93785eb
                              - 8b0e72479260324334e13d3c56249eb9
                              - e7f14e3d27bae40492ee67be4931910f
                              - 1104b44193b967c6a85ab6ef12632f0e
                              - e70b56fd2df88cebcdb44890716e824d
                              - 1772c7a954c0ee15b4621ff990e71ae8
                    else:
                      - if:
                          - condition: numeric_state
                            entity_id: sensor.battery
                            below: 90
                        then:
                          - service: >-
                              script.inovelli_blue_vzm31_sn_animated_notifications_zha
                            metadata: {}
                            data:
                              led: All
                              color: Teal
                              level: 100
                              effect: Pulse
                              duration: Indefinitely
                              target:
                                device_id:
                                  - c8face0983b5ab8e6ca7935bcce579bf
                                  - 0653d29eb9407d682856d13d0208937c
                                  - 62e5da15a2b75c09dbca0d9cc73e6a71
                                  - 4d328b3aa26b9354955a6450c590e101
                                  - cef2485fe7542cafdbd63d96adeae393
                                  - 3abdf970845325f6e9970e24bbf5b83e
                                  - 19e870bc60c4eb263fcd0e42e721a535
                                  - 4bf83af2c19f1b57a0b97c16c93785eb
                                  - 8b0e72479260324334e13d3c56249eb9
                                  - e7f14e3d27bae40492ee67be4931910f
                                  - 1104b44193b967c6a85ab6ef12632f0e
                                  - e70b56fd2df88cebcdb44890716e824d
                                  - 1772c7a954c0ee15b4621ff990e71ae8
                        else:
                          - service: >-
                              script.inovelli_blue_vzm31_sn_animated_notifications_zha
                            metadata: {}
                            data:
                              led: All
                              color: Blue
                              level: 100
                              effect: Pulse
                              duration: Indefinitely
                              target:
                                device_id:
                                  - c8face0983b5ab8e6ca7935bcce579bf
                                  - 0653d29eb9407d682856d13d0208937c
                                  - 62e5da15a2b75c09dbca0d9cc73e6a71
                                  - 4d328b3aa26b9354955a6450c590e101
                                  - cef2485fe7542cafdbd63d96adeae393
                                  - 3abdf970845325f6e9970e24bbf5b83e
                                  - 19e870bc60c4eb263fcd0e42e721a535
                                  - 4bf83af2c19f1b57a0b97c16c93785eb
                                  - 8b0e72479260324334e13d3c56249eb9
                                  - e7f14e3d27bae40492ee67be4931910f
                                  - 1104b44193b967c6a85ab6ef12632f0e
                                  - e70b56fd2df88cebcdb44890716e824d
                                  - 1772c7a954c0ee15b4621ff990e71ae8
mode: single

Combined, I’m pretty sure the issue is in this bit, somehow:

  - type: issue_all_led_effect
    domain: zha
    device_id: 0653d29eb9407d682856d13d0208937c
    effect_type: Pulse
    color: '{{ states("var.color") }}'
    level: 100
    duration: 255

Bizarre. Does it work with a numeric color value?

Confirmed-- set the color to 100, no issues. So, that line seemingly is the issue.