I have a live power consumption / return to grid sensor sensor.total_power_usage that has negative and positive values , is there a way to create a utility meter/template to add only negative values and ignore the positive values ?
I would like to know the same thing.
I guess you could use a template sensor to ignore the positive values and make negative values positive. You should then be able to pass that to your utility meter.
template:
- sensor:
- name: Negative power only
unit_of_measurement: "W"
device_class: power
state: "{{ -1 * states('sensor.total_power_usage') if states('sensor.total_power_usage') < 0 else 0 }}"
The sensor has positive and negative values , but i want to sum up only when is negative.
It doesn’t work the way above! the value is unavailable
Is there anything in your logs?
What happens if you put the template "{{ -1 * state('sensor.total_power_usage') if state('sensor.total_power_usage') < 0 else 0 }}"
in the template editor in developer tools? (Replace sensor.total_power_usage
with the name of your sensor.)
My sensor name is sensor.total_power_usage'
and at this time has a negative value of -735 but in template editor i get
It should be states('sensor.total_power_usage')
You are missing a s
in the states
.
i figure it out for the s but now is different error
TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
Add | int
to convert it to integer before doing a multiplication.
If i set this
{{ states(‘sensor.total_power_usage’) if states(‘sensor.total_power_usage’) | int < 1 else 0 }}
i get a negative value correctly now
if i try to multiply with -1 i get zero , from here i think i can figure it out
Thanks for your help