Where are the simple math sensors

I’ve been wondering for a while why we don’t have simple maths sensors, stuff like sum and substract?
I know template sensors are possible, but I think we can all agree that a proper sensor is better than having to do something like this:

value_template: >-
  {{ states('sensor.value_1') | float -
      states('sensor.value_2') | float + 
      states('sensor.value_3') | float }}

Oh I like that! Some math helpers that can do those things quick and easy would be nice indeed!

Wouldn’t the yaml be more complex than the template? like:

entity_id: sensor.value_1
operation: minus
on: 
  entity_id: sensor.value_2
  operation: plus
  on: sensor.value_3

It would have to look something like this right? Otherwise you can’t track order of operations or do multiple operations at the same time.

I was thinking it could be a list op operations, executed from start to bottom
Yaml might indeed look complicated, but I think it’s way easier (and less error prone) than templates in the end.
Also, if a nice UI can be build around that integration setup…:wink:

haha, I think I’m catching on :stuck_out_tongue:

Until math sensors arrive (if ever), you can do a few things fairly efficiently with templates. For example this gets the sum of all sensors whose device_class is ‘temperature’ (would be a better example with energy sensors but I have none):

{{ states.sensor
   | selectattr('attributes.device_class', 'eq', 'temperature')
   | map(attribute='state') | map('float') | sum }}

Screenshot from 2020-08-18 13-56-34


EDIT

Getting the average is just a little more work:

{% set x =  states.sensor
   | selectattr('attributes.device_class', 'eq', 'temperature')
   | map(attribute='state') | map('float') | list %}

Average: {{ (x | sum) / x | count }} 

1 Like