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
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
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
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 }}