Gambix91
(Gambix91)
1
Hi All
I’m really struglling coding
I have a smart sensor bidirectional elecricity
This sensor has 2 entity:
- Current Load: Watt of electricity
- Flow of the current: Reverse or Forward
I didn’t find a way to integrate this sensor into my energy calculation
I would like to set up Grid flow: Negative if entity flow is “Reverse”; Positive if it is “Forward”
I create this code:
sensors:
friendly_name: Consumo Totale Casa
unit_of_measurement: 'W'
device_class: Energy
{{ if (states('sensor.flusso') "reverse" ) }}
value_template: "{{ -1 * (states('sensor.consumo_pinza')| float (0) }}"
unit_of_measurement: "W"
{{ else }}
value_template: "{{ (states('sensor.consumo_pinza')| float * 1) | int }}"
unit_of_measurement: "W"
{{ endif }}
It give me error into code but I do not know what is wrong
Thank you
123
(Taras)
2
You can’t use use a Jinja2 template to control which YAML options are used in the configuration.
A Jinja2 template can only be used to compute the value of a YAML option.
sensors:
friendly_name: Consumo Totale Casa
unit_of_measurement: 'W'
device_class: energy
value_template: >
{{ states('sensor.consumo_pinza') | float(0) * iif(is_state('sensor.flusso', 'reverse'), -1, 1) }}
Gambix91
(Gambix91)
3
Thank you for your answer
Using your code Now I get this:

Value is positive and it is correct
So now entity shows Forward (means I get power from the grid) and it is correct to be positive value
I’ve tried to change to “forward” in code, just to check if value become negative, but it does not
123
(Taras)
4
I believe that’s how you want it to work, yes? Negative when ‘reverse’ and positive when ‘forward’.
Gambix91
(Gambix91)
5
I understood why!
The “REVERSE” or “FORWARD” text message from sensor is all capital letters.
Written in small letter, was not working reporting only “no” values
thank you so much!
1 Like