yousaf465
(Yousaf465)
April 22, 2022, 5:44am
1
I want to calculate the percentage of energy produced vs estimated energy production estimated by the solar forecast integration.
both sensor are
sensor.daily_yield
and
sensor.energy_production_today
how will I calculate the percentage for the energy produced?
yousaf465
(Yousaf465)
April 22, 2022, 6:04am
3
so In my case it will
sensor:
- platform: template
sensors:
my_sensor:
value_template: >-
{% set t = states('sensor.daily_yield') | float %}
{% set u = states('sensor.energy_production_today') | float %}
{{ (t / u) x 100 }}
Try it out in under Configuration > Template first
And … note that this is the ‘old’ way of writing template sensors (it will work though)
The new one is here, using state instead of value_template:
Template - Home Assistant (home-assistant.io)
yousaf465
(Yousaf465)
April 22, 2022, 6:10am
5
can’t find configuration > template.
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily yield = states('sensor.daily_yield') | float %}
{% set Estimated production today = states('sensor.energy_production_today') | float %}
{{ ((daily yield /Estimated production today ) * 100) | round(1, default=0) }}
tom_l
April 22, 2022, 6:16am
6
That’s because it is Developer Tools → Templates
You should add default values for your float filters and add an availability template to prevent strange readings if one or more of your sensors are unavailable.
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily yield = states('sensor.daily_yield') | float(0) %}
{% set Estimated production today = states('sensor.energy_production_today') | float(1) %}
{{ ((daily yield /Estimated production today ) * 100) | round(1, default=0) }}
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none ] }}
1 Like
yousaf465
(Yousaf465)
April 22, 2022, 6:17am
7
tom_l:
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily yield = states('sensor.daily_yield') | float(0) %}
{% set Estimated production today = states('sensor.energy_production_today') | float(1) %}
{{ ((daily yield /Estimated production today ) * 100) | round(1, default=0) }}
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none ] }}
thanks
I tried it there but got this error
TemplateSyntaxError: expected token 'end of statement block', got 'yield'
tom_l
April 22, 2022, 6:21am
8
You can’t have spaces in your variable names. Use something like this:
{% set daily_yield = states('sensor.daily_yield') | float(0) %}
{% set estimated_production_today = states('sensor.energy_production_today') | float(1) %}
Change them everywhere you’ve used them.
yousaf465
(Yousaf465)
April 22, 2022, 6:24am
9
changed code to this
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily_yield = states('sensor.daily_yield') | float(0) %}
{% set Estimated_production_today = states('sensor.energy_production_today') | float(1) %}
{{ ((daily_yield /Estimated_production_today ) * 100) | round(1, default=0) }}
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none ] }}
got this
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
33.2
availability: >
True
tom_l
April 22, 2022, 6:28am
10
Looks good. If the availability template changes to False
(because one of the sensors is unavailable) your main sensor will report unavailable
and not pollute your sensor history with incorrect readings.
Actually as you are doing division, you should check that the denominator is not zero as well, as division by zero will throw an error:
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none, 0 ] }}
yousaf465
(Yousaf465)
April 22, 2022, 6:30am
11
added it to configuration.yaml it will be available after a restart?
tom_l
April 22, 2022, 6:31am
12
Yes. Or a reload of templates.
Do a config check before doing either.
yousaf465
(Yousaf465)
April 22, 2022, 6:32am
13
how to do a reload of templates?
tom_l
April 22, 2022, 6:34am
14
Reload Template Entities near the bottom of the list on the Configuration / Settings / Server Controls Page.
1 Like
yousaf465
(Yousaf465)
April 22, 2022, 6:36am
15
got this error
Logger: homeassistant.config
Source: config.py:454
First occurred: 11:35:51 AM (2 occurrences)
Last logged: 11:35:51 AM
Invalid config for [sensor.template]: invalid slug template percentage calulated (try template_percentage_calulated) for dictionary value @ data['sensors']. Got OrderedDict([('template_grid_feed_in', OrderedDict([('friendly_name', 'Solar 2 Grid'), ('unit_of_measurement', 'W'), ('device_class', 'power'), ('value_template', "{% if states('sensor.grid_active_power') | int > 0 %}\n {{ states('sensor.grid_active_power') }}\n{% else -%}\n 0\n{% endif %}\n")])), ('template_grid_consumption', OrderedDict([('friendly_name', 'Grid 2 House'), ('unit_of_measurement', 'W'), ('device_class', 'power'), ('value_template', "{% if states('sensor.grid_active_power') .... (See /config/configuration.yaml, line 148). Please check the docs at https://www.home-assistant.io/integrations/template
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 188). Please check the docs at https://www.home-assistant.io/integrations/sensor
tom_l
April 22, 2022, 6:46am
16
template:
goes in your configuration.yaml file, not your sensors.yaml file.
yousaf465
(Yousaf465)
April 22, 2022, 6:50am
17
yousaf465:
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily_yield = states('sensor.daily_yield') | float(0) %}
{% set Estimated_production_today = states('sensor.energy_production_today') | float(1) %}
{{ ((daily_yield /Estimated_production_today ) * 100) | round(1, default=0) }}
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none ] }}
but can’t find it in developers tools, did reload the templates entities
tom_l
April 22, 2022, 6:56am
18
Why did you change this:
template:
- sensor:
- name: "Percentage of energy produced"
etc...
To this invalid config?
1 Like
yousaf465
(Yousaf465)
April 22, 2022, 6:57am
19
it was causing a error with my other template sensors created previously
yousaf465
(Yousaf465)
April 22, 2022, 7:00am
20
yousaf465:
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily_yield = states('sensor.daily_yield') | float(0) %}
{% set Estimated_production_today = states('sensor.energy_production_today') | float(1) %}
{{ ((daily_yield /Estimated_production_today ) * 100) | round(1, default=0) }}
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none ] }}
finally it worked
code used
#percentage of enrgy produced
template:
- sensor:
- name: "Percentage of energy produced"
unit_of_measurement: "°%"
state: >
{% set daily_yield = states('sensor.daily_yield') | float(0) %}
{% set Estimated_production_today = states('sensor.energy_production_today') | float(1) %}
{{ ((daily_yield /Estimated_production_today ) * 100) | round(1, default=0) }}
availability: >
{{ states('sensor.daily_yield') not in [ unknown, unavailable, none ] and
states('sensor.energy_production_today') not in [ unknown, unavailable, none ] }}