Caculate new sensor values with if conditions

Hello,

I am not skilled at all in the languages used here to define sensors, values etc… so i try by examples…

here below is my current configuration yaml and i want to add a computed value which will be a difference between 2 values, but i want it to be zero if the difference is negative and be the value if the difference is positive.

something like : If A-B< 0 then C = 0, if A-B> or = 0 then C = A-B

# Loads default set of integrations. Do not remove.

default_config:

# Text to speech

tts:

  - platform: google_translate

automation: !include automations.yaml

script: !include scripts.yaml

scene: !include scenes.yaml

homeassistant:

  packages: !include_dir_named packages

# Example configuration.yaml entry

sensor:

  - platform: keyatome

    username: !secret atome_uid

    password: !secret atome_pwd

  - platform: template

    sensors:

        legrand_conso_total:

            friendly_name: "Legrand Conso total"

            unit_of_measurement: 'kWh'

            value_template: '{{ states("sensor.legrand_puissance_circuit_1") |float + states("sensor.legrand_puissance_circuit_2") | float + states("sensor.legrand_puissance_circuit_3") | float + states("sensor.legrand_puissance_circuit_4") | float}}'

My Legrand sensor is fully detailed in a package

I have added a new sensor value legrand_conso_total which does the total of the 4 sensors values defined in the package. This seems working well.

Now i want a new value which will be the difference between two others only if the difference is >= 0

Can you help me understanding how i can write that ?
I suppose that it cabn be written in the same way as the value.template: '{{…}} used for this addition… but which syntax ?

And sorry if my yaml is not formatted correctly, i tried to use the ``` before and after but not sure it was the right way :slight_smile:

I would write it in that way:

value_template: >-
  {% set sensorA_value = states('sensor.yoursensorA') | float(0) %}
  {% set sensorB_value = states('sensor.yoursensorB') | float(0) %}
  {% set calculated_value = sensorA_value - sensorB_value | float(0) %}
  {% if calculated_value < 0 %}
      {% set calculated_value = 0 | float(0) %}
  {% endif %}
  {{ calculated_value | float(0) }}

A short test in the development tools, where you can create templates, this was working:

A = 12.5
B = 1.0

check if calculation > 0 => returned 0
check if calculation < 0 => returned 11.5

Or better yet use the new template integration rather than the legacy sensor template platform:

template:
  - sensor:
      - name: Test
        unit_of_measurement: 'kWh'
        device_class: energy
        state: >
          {% set calculated_value = states("sensor.legrand_puissance_circuit_1") | float(0) - states("sensor.legrand_puissance_circuit_2") | float(0) %}
          {{ calculated_value if calculated_value > 0 else 0 }}

thanks for answers but now i am unable to understand how to write these commands in my yaml current file, i get errors each time i try to write it, i took the second example from tom_I

here my yaml as of now :

sensor:
  - platform: keyatome
    username: !secret atome_uid
    password: !secret atome_pwd
  - platform: template
    sensors:
        legrand_conso_total:
            friendly_name: "Legrand Conso total"
            unit_of_measurement: 'kWh'
            value_template: '{{ states("sensor.legrand_puissance_circuit_1") |float + states("sensor.legrand_puissance_circuit_2") | float + states("sensor.legrand_puissance_circuit_3") | float + states("sensor.legrand_puissance_circuit_4") | float}}'

Well, no you use a template platform. I provided config for the template integration. Just put this all in your configuration.yaml file:

Please provide the full error text.

Ok sorry, got it

now its working

with this coding, but i am really unclear on how to code these templates here, i should find a tuto explaing the basics…
for example, i have sensor line 17, followed by platform: template and my addition for value legrand_conso_total
and after i have your coding with template and two sensor values test and test2

are these templates correctly writte…

# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
  - platform: google_translate
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
  packages: !include_dir_named packages
# Example configuration.yaml entry
sensor:
  - platform: keyatome
    username: !secret atome_uid
    password: !secret atome_pwd
  - platform: template
    sensors:
        legrand_conso_total:
            friendly_name: "Legrand Conso total"
            unit_of_measurement: 'kWh'
            value_template: '{{ states("sensor.legrand_puissance_circuit_1") |float + states("sensor.legrand_puissance_circuit_2") | float + states("sensor.legrand_puissance_circuit_3") | float + states("sensor.legrand_puissance_circuit_4") | float}}'
template:
  - sensor:
      - name: Test
        unit_of_measurement: 'kWh'
        device_class: energy
        state: >
          {% set calculated_value = states("sensor.legrand_puissance_circuit_1") | float(0) - states("sensor.legrand_puissance_circuit_2") | float(0) %}
          {{ calculated_value if calculated_value > 0 else 0 }}
  - sensor:
      - name: Test2
        unit_of_measurement: 'kWh'
        device_class: energy
        state: >
          {% set calculated_value = states("sensor.legrand_puissance_circuit_2") | float(0) - states("sensor.legrand_puissance_circuit_3") | float(0) %}
          {{ calculated_value if calculated_value > 0 else 0 }}      

so now i have rewritten my config yaml like this (template section) , it seems better organized, and its still working… :slight_smile:

# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
  - platform: google_translate
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
  packages: !include_dir_named packages
# Example configuration.yaml entry
sensor:
  - platform: keyatome
    username: !secret atome_uid
    password: !secret atome_pwd
template:
  - sensor:
      - name: Test
        unit_of_measurement: 'kWh'
        device_class: energy
        state: >
          {% set calculated_value = states("sensor.legrand_puissance_circuit_1") | float(0) - states("sensor.legrand_puissance_circuit_2") | float(0) %}
          {{ calculated_value if calculated_value > 0 else 0 }}
  - sensor:
      - name: Test2
        unit_of_measurement: 'kWh'
        device_class: energy
        state: >
          {% set calculated_value = states("sensor.legrand_puissance_circuit_2") | float(0) - states("sensor.legrand_puissance_circuit_3") | float(0) %}
          {{ calculated_value if calculated_value > 0 else 0 }}
  - sensor:
      - name: legrand_conso_total
        unit_of_measurement: 'kWh'
        device_class: energy
        state: >
          {% states("sensor.legrand_puissance_circuit_1") |float + states("sensor.legrand_puissance_circuit_2") | float + states("sensor.legrand_puissance_circuit_3") | float + states("sensor.legrand_puissance_circuit_4") | float %}
1 Like