Change color of light depending on state of charge of solar batteries

We have solar and a battery bank with inverter for our electricity and I’d like to have one of our RGB ceiling lights act as a status indicator for the state of charge for our batteries.
I have created an automation as per following, but it doesn’t seem to work… :frowning_face:
I’ve looked at this for some time now but am now stuck with what else I should try.

Current automation set-up so far:

sensor:
  - platform: template
    sensors:
      soc_light_colour:
        value_template: >
          {% set soc = states('sensor.batterystateofcharge')|float %}
          {% set red = ((100 - soc) * 2.55)|int %}
          {% set green = (soc * 2.55)|int %}
          {{ '[' ~ red ~ ',' ~ green  ~ ',0]' }}

automation:
  - alias: Light indicator for SOC
    trigger:
    - platform: state
      entity_id: sensor.soc_light_colour
    action:
    - service: light.turn_on
      data_template:
        entity_id: light.officelight2
        brightness_pct: 75
        rgb_color: "{{ states('sensor.soc_light_colour') }}"

The problem is that this "{{ states('sensor.soc_light_colour') }}" is a string (text), but rgb_color: expects a list.

v0.117 has introduced python types in templates for specifically this type of problem. But you have to enable it as it is a beta feature. See: https://www.home-assistant.io/blog/2020/10/28/release-117/#native-types-support-for-templates-beta

Thanks Tom… I’d have never worked that out :exploding_head:

Instead of using the python templates with little documentation and examples available I changed tack and used a send mqtt message action instead. That is working now. Just have to fiddle with the colours so I can see them well enough… (my colour perception is a bit impaired :wink:)

Anyway, for others, following is the automation that is working for me currently:

sensor:
  - platform: template
    sensors:
      soc_light_colour:
        value_template: >
          {% set soc = states('sensor.batterystateofcharge')|float %}
          {% set red = ((100 - ((soc - 50) * 2)) * 2.55)|int %}
          {% set green = ((soc - 50) * 2 * 2.55)|int %}
          {{ red ~ ',' ~ green ~ ',0' }}

automation:
  - alias: Light indicator for SOC
    trigger:
    - platform: state
      entity_id: sensor.soc_light_colour
    action:
    - service: mqtt.publish
      data:
        topic: cmnd/officelight2/Color2
        payload_template: "{{ states('sensor.soc_light_colour') }}"
    - service: mqtt.publish
      data:
        topic: cmnd/officelight2/Dimmer
        payload: 100