Help with using a sensor value for an action value

My goal (big picture): I want to build a device that shows my current solar panel power output and the charge level of my battery by lighting up two LED stripes accordingly.

The plan: An add-on (alphaess) sends data to MQTT every minute (works :white_check_mark:), the device runs WLED and using the effect mode “Percent” I’m able to set numbers in the range of 0 to 100 (works :white_check_mark:).

For testing purposes with 12 LED I set up 13 automations and calculated the below/above conditions for each possible number of LEDs (0 to 12). Works but clumsy, since the state of charge sensor provides numbers from 0 to 100 and the WLED effect “Percent” expects numbers from 0 to 100 :slight_smile:

I read about a similar question in How to set a the value in an automation from the trigger obje

So I try to setup an automation, triggered by new data on topic alphaess (happens ~ every minute), no conditions and set an action on the WLED device, reading the sensor value:

alias: Set Solar Battery State Of Charge
description: ''
mode: single
trigger:
  - platform: mqtt
    topic: alphaess
condition: []
action:
  - device_id: 1881e924e624f2b9196c8b6085ea4383
    domain: number
    entity_id: number.wled_segment_1_intensity
    type: set_value
    value: '{{ states(''sensor.solar_battery_soc'') | float }}'

That configuration refuses with the error message Message malformed: expected float for dictionary value @ data[‘value’]

This is my sensor definition:

sensor:
  - platform: mqtt
    name: "Solar Battery SOC"
    state_topic: "alphaess/ALAnnnnnnnnnnn"
    unit_of_measurement: '%'
    icon: mdi:battery
    value_template: "{{ value_json.soc }}"

Any advice on how to set the value?

Almost certainly the problem is that the device action doesn’t support templates.

You should use the number.set_value service call instead

action:
  - service: number.set_value
    data:
      entity_id: number.wled_segment_1_intensity
      value: '{{ states(''sensor.solar_battery_soc'') | float }}'
1 Like

Thank you, @Tinkerer that works like a charm :slight_smile:

1 Like