Calculate 2 sensors won't work

I have 2 sensors who I wants to be a total:
sensor.kosten_airco_woonkamer + sensor.kosten_airco_boven = sensor. totale_kosten
sensor.kosten_airco_woonkamer and sensor.kosten_airco_boven are working correct, but Sensor.total_kosten is not available :frowning: I tried a restart. I checked the configuration.yaml (no errors)

Can anyone help?

configuration yaml

#energie kosten airco
input_number:
  prijs_per_kwh:
    name: "Prijs per kWh"
    initial: 0.33  # Stel de prijs in, bijvoorbeeld 0,22 EUR/kWh
    min: 0
    max: 1
    step: 0.01
    unit_of_measurement: "EUR/kWh"

sensor:
  - platform: template
    sensors:
      kosten:
        friendly_name: "kosten airco woonkamer"
        unit_of_measurement: "EUR"
        value_template: "{{ (states('sensor.dag_verbruik_woonkamer') | float) * (states('input_number.prijs_per_kwh') | float) }}"

      kosten_boven:
        friendly_name: "kosten airco boven"
        unit_of_measurement: "EUR"
        value_template: "{{ (states('sensor.dag_verbruik_boven') | float) * (states('input_number.prijs_per_kwh') | float) }}"

      totale_kosten:
        friendly_name: "totale kosten airco"
        unit_of_measurement: "EUR"
        value_template: "{{ (states('sensor.kosten_airco_woonkamer') | float) + (states('sensor.kosten_airco_boven') | float) }}""

There are too many double quotes at the end of this template. Try:

value_template: "{{ states('sensor.kosten_airco_woonkamer') | float + states('sensor.kosten_airco_boven') | float }}"

Didn’t work :frowning:

explain what didn’t work.

1 Like

I made screenshots to explain me further: There is a value total costs, but when I try to make a statistic there is no statistics available see screenshots:



for statistics to work, you need to swap to the new template configuration and make sure you use a device_class, state_class, and valid unit_of_measurement.

Do you have a suggestion how I can do this?

Hi Gert-Jan,

This is the format.

Ok I rebuild the yaml to the new template. But now I have new problem. De total is 0 so something went wrong with the calculations. I don’t understand why it’s giving 0. Because the other values are set.

# Energie kosten airco
input_number:
  prijs_per_kwh:
    name: "Prijs per kWh"
    initial: 0.33  # Stel de prijs in, bijvoorbeeld 0,22 EUR/kWh
    min: 0
    max: 1
    step: 0.01
    unit_of_measurement: "EUR/kWh"

template:
  sensor:
    - name: "kosten airco woonkamer"
      unit_of_measurement: "EUR"
      value_template: "{{ (states('sensor.dag_verbruik_woonkamer') | float(0)) * (states('input_number.prijs_per_kwh') | float(0)) }}"
      state_class: total_increasing
      
    - name: "kosten airco boven"
      unit_of_measurement: "EUR"
      value_template: "{{ (states('sensor.dag_verbruik_boven') | float(0)) * (states('input_number.prijs_per_kwh') | float(0)) }}"
      state_class: total_increasing
      
    - name: "totale kosten airco"
      unit_of_measurement: "EUR"
      value_template: "{{ (states('sensor.kosten_airco_woonkamer') | float(0)) + (states('sensor.kosten_airco_boven') | float(0)) }}"
      state_class: total_increasing

here de debug output

# Energie kosten airco
input_number:
  prijs_per_kwh:
    name: "Prijs per kWh"
    initial: 0.33  # Stel de prijs in, bijvoorbeeld 0,22 EUR/kWh
    min: 0
    max: 1
    step: 0.01
    unit_of_measurement: "EUR/kWh"

template:
  sensor:
    - name: "kosten airco woonkamer"
      unit_of_measurement: "EUR"
      value_template: "1.8479999999999999"
      state_class: total_increasing
      
    - name: "kosten airco boven"
      unit_of_measurement: "EUR"
      value_template: "0.46199999999999997"
      state_class: total_increasing
      
    - name: "totale kosten airco"
      unit_of_measurement: "EUR"
      value_template: "0"
      state_class: total_increasing

I think you pasted the wrong thing in the debug output. It would be helpful to see the values from developer tools.

You do know that pasting the YAML into the template editor doesn’t actually create the sensors in the system, don’t you?

Your final sensor template can’t reference the values from the sensors above. You must put this code into your configuration.yaml.

Yes I know that. Just pasting the same code as de configuration.yaml for debugging. Since the total is not working …

Does sensor.kosten_airco_woonkamer exist? You have to do a full restart when using a new integration like template.