Marchel
(Marchel)
August 17, 2022, 8:31pm
1
Can someone help me get the following into yaml correctly ?
In `the development template it looks good, but I can’t get it into yaml without errors.
- sensor:
- name: "Total power use"
{% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
state: "{{states('sensor.power_consumption')|float}}"
{% else %}
state: "{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
{% endif %}
browetd
(Browet Didier)
August 17, 2022, 8:48pm
2
Try this:
- sensor:
- name: "Total power use"
state: >
{% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
"{{states('sensor.power_consumption')|float}}"
{% else %}
"{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
{% endif %}
Marchel
(Marchel)
August 17, 2022, 9:03pm
3
thank you,
So I had to add state: > ?
But now get the message:
invalid config for [template]: [name] is an invalid option for [template]. Check: template->name. (See /config/configuration.yaml, line 58).
pepe59
(Pepe59)
August 17, 2022, 9:17pm
4
You can try it like this:
- platform: template
sensors:
total_power_use:
friendly_name: Total power use
value_template: >-
{% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
"{{states('sensor.power_consumption')|float}}"
{% else %}
"{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
{% endif %}
browetd
(Browet Didier)
August 17, 2022, 9:21pm
5
This code should be in the template sensors… Something like this:
template:
- sensor:
- name: "Total power use"
state: >
{% if states('sensor.omvormer_active_power') in ("unavailable", "unknown") %}
"{{states('sensor.power_consumption')|float}}"
{% else %}
"{{states('sensor.omvormer_active_power')|float/1000 + states('sensor.power_consumption')|float - states('sensor.power_production')|float}}"
{% endif %}
See documentation:
The code proposed by @pepe59 is the legacy method (and should work) but is not the recommended method which is to move sensors under template… See the following link:
Hello,
I have several templates in my configuration (from multiple source) that work but are written differently
So I wonder what is the difference/best pratrice between .
sensor:
- platform: template
sensors:
energy_return:
AND
template:
- sensor:
- name: "energy_return"
Marchel
(Marchel)
August 17, 2022, 9:33pm
6
The last solution worked!
I’m going to read it all again tomorrow to understand.
Going to bed now I get up early tomorrow.
Thank you both very much,
Marchel
(Marchel)
August 18, 2022, 3:16pm
7
Did some fine tuning and everything seems to be working fine.
thanks again.
- sensor:
- name: "Total power use"
state: >-
{% if states('sensor.omvormer_active_power') in ('unavailable', 'unknown') %}
{{states('sensor.power_consumption')}}
{% else %}
{{states('sensor.omvormer_active_power')| float /1000 + states('sensor.power_consumption') | float - states('sensor.power_production') | float | round(3)}}
{% endif %}
unit_of_measurement: "KW
Marchel:
float
But you should add float defaults, according to this
Note
This topic was created prior to the release of 2021.10.0 and was meant to inform the community of important upcoming changes to certain aspects of templating.
After the release of 2021.10.0, petro posted many examples of how to adapt templates to comply with the new requirements.
I recommend reviewing the examples before posting questions.
I skimmed the pre-release notes and selected several notable templating improvements and modifications.
Breaking Change
Several existing func…
1 Like
Marchel
(Marchel)
August 19, 2022, 7:39am
9
If I may ask the question here:
default float
If I understand correctly, the default value is the value that the status should assume if no numerical value is received from a sensensor ?
Yes and no. But not the status overall, but only the part, where you use the float filter. And you have some of them. If only one state is not there, the calculation will not work without defaults for float-filters.
Marchel
(Marchel)
August 19, 2022, 8:31am
11
Thank you,
If I understand correctly, the default values will prevent you from getting errors in your log?
In addition, I should write a separate routine if this error occurs.
For example in the above : a default value of -13.
If a default value of -13 is assigned then a sensor is not working properly then do this…