I have a working developer template which uses ‘if’ ‘then’ ‘else’ statements to show a number from my mains power sensors.
When I try to import it into the configuration.yaml under template: -sensor: it doesn’t show an error, but also doesn’t show the expected value.
Extremely long story as short as I can get it:
My 3 phase mains sensors give me a power output in watts, phase 2 shows the value of my solar panels and whatever else I’m consuming but only in a cumulative sense (no direction), so if it’s night and I’m consuming 100w, it’ll show 100w, but if it’s daytime, and I’m generating 1000w, it’ll show 1000w.
I’ve used the developer | template section in HA to output a mathematical solution to allow me a (largely) accurate output using IF, THEN, ELSE statements, but as I need to show this output as a sensor in configuration.yaml, it allows it to load, but won’t show an output.
Any ideas?
Template Jinja2 code:
{% if Fronius is lt Phase2 %} {% set CorrectedPH2Watts = Phase2 - Fronius %} {% else %} {% set CorrectedPH2Watts = Phase2 * -1%} {% endif %} {{CorrectedPH2Watts}} W
Attempted YAML code:
- sensor:
- name: "Net Power Total"
unit_of_measurement: "W"
state: >
{% if int(states.sensor.fronius_primo_power_ac.state) is lt int(states.sensor.mainspower_ct2_watts.state) %}
int(states.sensor.mainspower_ct2_watts.state) - int(states.sensor.fronius_primo_power_ac.state)
{% else %}
int(states.sensor.mainspower_ct2_watts.state) * -1
{% endif %}
It is i possible to read your code if you do not put it in a code block, because now the forum changes the appearance. Please formt it properly next time. But what I do see is that you should not put the W for the unit in the template for the state. It should just be a number, now you have a text.
I also see the tested template is not what you put in the state. It seems like you commented something out the wrong way. In the intended one it seems to miss {{}} there for the output, but it is very hard to read now or correct it for you.
As Edwin says, please format code correctly for the forum. See here.
Your general templating style (referring to states) is not best practice. Read this, in particular the warning box at the bottom of the States section.
I’ve never seen anyone use the lttest rather than the <operator like that. Where did you get that from? Was it AI?
You don’t need to use YAML, you can create this as a Template Sensor Helper in the UI. Just use a state template of:
Firstly, sorry, I think I’ve corrected it now, I did try the ‘blockquote’ but I didn’t realise a. That it meant insert code here, or b. The preview could be shown after the helpful hints overlay was removed.
Second, it didn’t even occur to me I could use a template helper to do this, it works, it all works!
You’ve no idea how long I’ve been reading the wrong values, thinking I need to spend some real time figuring this out… and now it’s not only working in configuration.yaml, but I have it as a template sensor!
I can’t thank you all enough for the prompt (almost immediate) accurate and extremely helpful responses.
The ‘lt’ rather than ‘<’ came from example Jinja2 code I fell upon yesterday… I only found out yesterday that Jinja2 was what I was using in the Template window… and Jinja2 was in fact not YAML. I did Ai it, but it wasn’t very helpful.
Out of interest, would you recommend adding custom sensors like this to configuration.yaml, or adding them as template helpers? I have quite a few in configuration.yaml.