Struggling to combine a template sensor with an automation

Hi folks,
I’ve got a working template sensor (currently returning 96) but when I try to set another sensor to its value the automation says it’s not getting a float. Me confused!

Edited to remove screenshots and add code.

Automation:

alias: Set target SOC v03
description: ""
trigger: []
condition: []
action:
  - service: number.set_value
    data:
      value: states('sensor.sol_calc_target_soc') | float
    target:
      entity_id: number.givtcp_sd2227g949_target_soc
mode: single

Template Sensor:

template:
  - sensor:
    - name: sys_sol_active_forecast
      unit_of_measurement: "kWh"
      state: >
        {% if now().hour < 7 %} {{ states('sensor.energy_production_today') }}
        {% else %} {{ states('sensor.energy_production_tomorrow') }}
        {% endif %}
    - name: "sol_calc_target_soc"
      unit_of_measurement: "%"
      state: >
        {% set af = states('sensor.sys_sol_active_forecast') | float %}
        {% set ls = states('input_number.sys_sol_low_solar') | float %}
        {% set hs = states('input_number.sys_sol_high_solar') | float %}
        {% set maxsoc = states('input_number.sys_sol_max_soc') | float %}
        {% set minsoc = states('input_number.sys_sol_min_soc') | float %}
        
        {% if af < ls %} {{ maxsoc }}
        {% elif af > hs %} {{ minsoc }}
        {% else %} {{ maxsoc - (af - ls) / (hs - ls) * (maxsoc - minsoc) }}
        {% endif %}

The error is:
Stopped because an error was encountered at 29 January 2023 at 12:27:54 (runtime: 0.02 seconds)
expected float for dictionary value @ data[‘value’]

I’ve tried (because I’ve seen lots of examples with different formats):

{{ }} around it all.
“{{ }}” around it all.
With and without state()
With and without single quotes around the sensor name.
With and without sensor.

Maybe I’m missing a combination of the above?

Put your template in quotes and double braces, see: Templating - Home Assistant

Also please don’t post pictures of text. Post the text correctly formatted.

Sorry, I thought that would show the indentation better. My bad. I’ll read up on how to format it correctly and do that.

Because it is a picture I can’t copy and paste it into a new post and edit it to show you how.

Completely understand. I should have checked. Sorry. Now done properly I think.

1 Like
action:
  - service: number.set_value
    data:
      value: "{{ states('sensor.sol_calc_target_soc') | float(0) }}"
    target:
      entity_id: number.givtcp_sd2227g949_target_soc

I would have bet a lot of money I’d tried “{{ }}” but I tried again and it worked. Thank you.
Maybe when I tried before I didn’t have the float in there too.

1 Like