Better, more robust, way to create these template sensors?

HI,

After carefully setting up all kinds of sensors reading real devices, im finally getting to creating several arithmetic ‘visual’ sensors, based upon those real devices.

Still im struggling a bit, and most of all, believe there should be a better, more reliable, less hard coded way of doing so. I was looking for a way to add all sensors with the ‘actueel’ in it, but couldn’t find a working solution in the templates. Hence, i coded calculated_actual_usage_switches like this for now, and its working beautifully , that is not the issue.

Please heave a look for me how to make this better. and easier to add new or changed sensors in the future. (have found ou the hard way that hard coding these sensors is prone to problems when setup changes…

below is looking simpler than it really is, since all sensors are based on mqtt sensors themselves, and feeding the complete hub’s data into the Homeassistant. Nevertheless, it must be possible to optimize the code :wink:

##########################################################################################
# Berekende waardes van verbruik
##########################################################################################

    calculated_actual_usage_switches:
      friendly_name: 'Actueel gebruik switches'
      unit_of_measurement: 'Watt'
      value_template: '{{ ((states.sensor.audio_auditorium_actueel.state |int)
                          + (states.sensor.audio_gym_actueel.state |int)
                          + (states.sensor.boiler_bijkeuken_actueel.state |int)
                          + (states.sensor.cv_garage_actueel.state |int)
                          + (states.sensor.cv_stookhok_actueel.state |int)
                          + (states.sensor.cv_zolder_actueel.state |int)
                          + (states.sensor.dorm_actueel.state |int)
                          + (states.sensor.espresso_keuken_actueel.state |int)
                          + (states.sensor.freezer_bijkeuken_actueel.state |int)
                          + (states.sensor.inductieplaat_actueel.state |int)
                          + (states.sensor.quooker_replace_actueel.state |int)
                          + (states.sensor.tester_actueel.state |int)
                          + (states.sensor.vaatwasser_keuken_actueel.state |int)
                          + (states.sensor.wasdroger_bijkeuken_actueel.state |int)
                          + (states.sensor.wasmachine_bijkeuken_actueel.state |int)
                          + (states.sensor.zonneboiler_zolder_actueel.state |int)) }}'

    calculated_nett_usage:
      friendly_name: 'Netto verbruik'
      unit_of_measurement: 'Watt'
      value_template: '{{ ((states.sensor.stroom_actueel_verbruik.state |int)
                          - (states.sensor.solaredge_production.state |int)) }}'

    calculated_zp_minus_usage:
      friendly_name: 'Zp solar - actueel'
      unit_of_measurement: 'Watt'
      value_template: '{{ ((states.sensor.zp_solar.state |int)
                       - (states.sensor.stroom_actueel_verbruik.state |int)) }}'
    calculated_usage_minus_zp:
      friendly_name: 'Actueel - zp solar'
      unit_of_measurement: 'Watt'
      value_template: '{{ ((states.sensor.stroom_actueel_verbruik.state |int)
                       - (states.sensor.zp_solar.state |int)) }}'

    calculated_usage_minus_switches:
      friendly_name: 'Ongemeten verbruik'
      unit_of_measurement: 'Watt'
      value_template: '{{ ((states.sensor.stroom_actueel_verbruik.state |int)
                       - (states.sensor.calculated_actual_usage_switches.state |int)) }}'
                       
    calculated_verbruik_minus_switches:
      friendly_name: 'Ongemeten verbruik v2'
      unit_of_measurement: 'Watt'
      value_template: '{{ ((states.sensor.calculated_nett_usage.state |int)
                       - (states.sensor.calculated_actual_usage_switches.state |int)) }}'

Do those sensors have a unique attribute that is only available for them? If so, then you can use the sum function combined with the selectattr function. (note, I’ve never tried this myself)

{{ (states.sensor|selectattr("uniqueAttribute"))|sum }}

If you don’t, then due to scoping issues, you can easily loop through the data, but I don’t know how to sum it all together in Jinja.

EDIT: This would be relatively easy in Appdeameon or node-red if you feel like adding an extension to Home Assistant

they all are defined with the _actueel bit in them, though i don’t think thats an attribute?
they’re all and the only members of the group.iungo_switch_verbruik_actueel … need only to sum the values of these sensors.

i do have the node red add-on installed, but havent used it yet, since i didnt need it (i thought…) Maybe now’s a good opportunity to start. Please , if you know how to, give me a hint how i would have to go about realizing the above quest?

Cheer,s
Marius

Groups are just lists of entity_id names, so I don’t think you can use the sum function over them. I have a group of light level sensors that all return numbers and can’t figure out how to sum them.

node-red has a lot of capability, including javascript functionality. I’m using the “node-red-contrib-home-assistant” nodes and they do simple string matching. So, if you drop in a server-state-change node and set the entity_id filter to “_actueel”, then that will pump out a message for every state change for an entity with “_actueel” in the name. This is easy to see by connecting it to a debug node and printing the entire message.

From there you could shove it into a function block and save off the value for each switch whenever something changes. You’d have to make a map to store the data, but it should be relatively easy (i hope, I haven’t gotten that far). You would want to store everything in the flow context so it stays around for a while.

https://nodered.org/docs/writing-functions#flow-context

ok thanks, ill be sure to study that.
my first sensor in the post sums all values in the group, so if thats of any help, it works fine.

Cheers,
Marius

Have a look in this thread about looping through all entities that have a battery attribute…

… You may be able to convert the code examples there to your needs :+1: