Combine/ Multiply Boolean Switch with Entity value

Hi everyone,
I try to track the output of my Solar Battery. Therefore i have created mutliple things to do so (my battery currently does not offer any tracking).
The end result is a binary sensor which is switched on when the Battery is giving power, and switched off when the battery loads.
So now my problem:
I’d like to track the power, the battery is delivering to my house.
My simple idea would have been to create just a combination of sensors where i just multiply the boolean value of my switch with the measured energy.
Unfortunately not possible because it’s not possible to combine a boolean switch with an entity
Second try was to make a group of sensors. Also not possible because of the same
Third try was to make a template. Here i was out. i tried something, but couldn’t save it without an error.

Has anyone a clue?
The boolean switchs name is input_boolean.akku, the entities name is TSUM_HM800

BR and thanks a lot

Create a template sensor in the UI (under Helpers) with this as the state template:

{{ iif(is_state('input_boolean.akku', 'on'), states('sensor.tsum_hm800'), 0) }}

You can also use a filter on the boolean to convert it to a number, where it will be 0 or 1. So you use the template {{ (states('input_boolean.akku') | float) * (states('sensor.tsum_hm800')| float) }} to get a number that represents the value of the power delivered by the battery. This is assuming that your TSUM entity is a sensor with a state that represents the power. You could use a similar template with a (2*boolean - 1) arithmetic to make a sensor that tracks the net power to or from the battery.

Thanks. I now created both helpers and will report back this evening when the battery switches on.
Just for information (because this was more or less a question asked in your answers):
The switch delivers on or off
Where the sensor delivers a value in W


Unfortunately both did not work. They both do not contain any data :frowning:
Any new suggestions on how to combine Watts with boolean?


Show me the template sensor you created, and a screenshot of Developer Tools / States for your input boolean and your sensor.

There is no point looking for “new suggestions” until we’ve got to the bottom of this one. Both suggestions are sound and will work if implemented properly.

What is the actual entity ID for the sensor? We’ve both had to guess it as you didn’t include it. If our guess is wrong and you’ve blindly included it in a template sensor, that’s why it won’t work.

Is it sensor.tsum_hm800 or something else?


Thanks you are right. i’ll try to clarify, but i don’t find the location of where to access the sensor in yaml, so i hope the following screenshots help too (i could find them in /config/.storage/)

The developer output of the three sensors (my Accu-switch, and allard’s and your sensor) look like that:

The attributes of both of your sensors:

The GUI for Allards sensor (the text is like Allard suggested):

And a screenshot from yours (i used your template, but changed the iif to if):

Also the power sensor:

Also when making the screenshots i found that the name of my power sensor is sensor.tsum_hm800_power and not as originally stated sensor.tsum_hm800

I justcorrected this in the templates…

Hope this helps you helping me :smiley:
Thanks for the effort.

iif was not a typo. See here:

{{ iif(is_state('input_boolean.akku', 'on'),
       0) }}

Of course, if your power sensor is unavailable (as in the screenshot) and the input_boolean is on, then the template sensor will correctly report unavailable too. If you want it to show 0 in that situation:

{{ iif(is_state('input_boolean.akku', 'on') and 
       states('sensor.tsum_hm800_power') not in ('unknown', 'unavailable'),
       0) }}
1 Like

Using the correct name for the sensor is a must of course.
You can use the GUI in the ‘template’ tab of developer tools to check that your templates generate useful outputs, it’s a super handy tool.

If your TSUM sensor is unavailable you should specify some default value. Tbh I do not know by heart what the float filter does to unavailable values, I think it should default to 0 but am not certain if it doesn’t pass on the ‘unavailable’ state. See here for how to change the default value: Template Designer Documentation — Jinja Documentation (3.0.x)

Ok i learned something from you! Didn’t know about iif :slight_smile: For all programming languages i know this looks like a typo :smiley:
I just updated it according to your lates suggestion, and now it works.
It shows 0W when the akku switch is off and a value when switched on!

Thanks a lot!