Calculation with sensor values?

I want to calculate the percentage of energy produced vs estimated energy production estimated by the solar forecast integration.
image

both sensor are

sensor.daily_yield

and

sensor.energy_production_today

how will I calculate the percentage for the energy produced?

Have a look here:
Calculations with sensor values - Configuration - Home Assistant Community (home-assistant.io)

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)


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) }}  

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

thanks
I tried it there but got this error

TemplateSyntaxError: expected token 'end of statement block', got 'yield'

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.

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

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 ] }}

added it to configuration.yaml it will be available after a restart?

Yes. Or a reload of templates.

Do a config check before doing either.

how to do a reload of templates?

Reload Template Entities near the bottom of the list on the Configuration / Settings / Server Controls Page.

1 Like


image
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

template: goes in your configuration.yaml file, not your sensors.yaml file.


but can’t find it in developers tools, did reload the templates entities

Why did you change this:

template:
  - sensor:
      - name: "Percentage of energy produced"
        etc...

To this invalid config?
206c07484c754b7916d2c5e8b236f45b82ee9e02

1 Like

it was causing a error with my other template sensors created previously

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 ] }}