Solar Sensor Template (Offset Dates)

I want to check the production amount that my electrical distributor is reporting remotely, against how much my solar inverter is producing locally.

I use two components for solar:

  1. One for my Fronius Inverter which gives live values via WiFi.
  2. A webhook to my Solar Distributor’s page. All of it’s values are delayed by a day (the sensor itself is “Yesterday’s Product” and “Yesterday’s Usage”.

I’ve made a Sensor Template which subtracts yesterday’s consumption value from the distributor from today’s production value from the inverter:

- platform: template
  sensors:
    fronius_inverter_production_minus_easy_distributor_consumption:
      value_template: '{{(states.sensor.fronius_day_energy.state | float ) - (states.sensor.energyeasy_previous_day_consumption.state | float)}}'
      friendly_name: “Solar Difference”

How do I offset the sensor.fronius_day_energy by a day so that yesterday’s consumption is subtracted from that?

I’m thinking the elegant solution is if I can make a new sensor that is “yesterday’s production” (of the solar inverter).

The only way I can think of is:

  1. Create a statistics sensor using sensor.fronius_day_energy. Set the max_age to hours: 24. In this example we will assume it’s named sensor.fronius_last_24_hours.
  2. Create a date sensor that updates at midnight (default). It will default to the name sensor.date.
  3. Create a template sensor that pulls the total attribute from the statistic sensor. But you’ll have to get inventive with this to cause it to only update at midnight. The following template sensor should only update at midnight, meaning you’ll only have yesterdays results in it.
- platform: template
  sensors:
    fronius_yesterdays_energy:
      entity_id: sensor.date
      value_template: >
        {% set domain = 'sensor' %}
        {% set object_id = 'fronius_last_24_hours' %}
        {% set entity_id = '{}.{}'.format(domain, object_id) %}
        {{ state_attr(entity_id, 'total') }}

The reason we have to split the entity_id up is because we don’t want the template parser to create a listener for this sensor. If it creates a listener, it will update when the sensor updates and we don’t want that. We want it to only update at midnight. Using entity_id set to sensor.date should cause it to only update at midnight. Meaning this will always have yesterdays total energy in it.

2 Likes

Lots to learn for me here - thank you so much for your time. Will attempt this over the coming days/weeks and report back here.