How to create a new data entity based on two other entities

Hi,

I’ve some solar panel inverter that is providing the current solar production and the current power usage. I want to create two entities showing what is the current grid import and grid export.

I’ve been in home-assistant, Devices & services, helper.

I’ve created a new “Template”:

And specified a state template:

My template is the following:

{{ [0, states(sensor.production_electricite_actuelle) | int(0) - states(sensor.conso_actuelle) | int(0) ] | max }}

But after one day I’ve saved it, it still shows as “Unavailable”. I’ve copy-pasted the source sensor names from the entities list, so I’m pretty sure they have the correct values.

I tried several things, including setting my template entity in kw(since both my source entities are provided in kw), but same result.

Any idea what I messed up?

The argument for the states() function should be a string. You’ve supplied a variable which isn’t defined anywhere in your template. Add quotes around the entity_id to make it a string.

{{ [0, states('sensor.production_electricite_actuelle') | int(0) - states('sensor.conso_actuelle') | int(0) ] | max }}

Best practice is to put your template into the template editor (developer tools → template) and make sure it is giving you the results you expect, prior to creating the template sensor.

Amazing, I didn’t know about the template developer tools(actually about any developer tool), that make my life much easier(and you’re definitely correct about the string. Thank you very much!