Calculate amount of 'free' solar power that can be taken back from the grid at any time

Hi all,

I have solar panels on the roof of my house and that’s all good :wink:

My problem is that in Denmark where I live we only get pennies on the dollar when we export power to the grid. So when I do not use my power i have to sell it for 3,8 cents (usd) / kwh - when i use from the grid i pay 32 cents / kwh.

There is a small upside though - if I use power that i have exported WITHIN one hour i get it back for 3,8 cents.

Essentially this means that i have one hour of free storage on the grid (almost, I think there is still a small fee for transport).

image
(timescale: 30 hrs, legend, red: consumption, cyan: solar production)

So on a sunny day I end up producing quite a bit more than i use, but I will still pay full price for around 1/3 of the total power for the day.

Here is production (blue) vs consumption (red) over 3 days
image

I have a smappee.com sensor installed that feeds both production and consumption into HA and this works great (this is what is generating these charts)
image

So I am wondering if theres is a way of calculating the total amount of ‘free’ power available on the grid at any given time?

I want to use this information to learn/calculate/find out when I can ‘start the washing machine for free’ etc.

I imagine a ‘sensor’ that shows how much power is current available for free (eg 0,8 kwh) accompanied with a chart of when it was generated. No point in having lots of power if it runs out soon)

sry for the long post - and I hope that it all makes sense.

Please, let there be somebody out there who has solved this problem already or maybe have a smart idea on how to do the calculations in home assistant.

Br Peter

just subtract the two values in a template sensor

binary_sensor:
  - platform: template
    sensors:
      surplus:
        friendly_name: Power Surplus
        value_template: >
          {{ (states('sensor.production') | float - states('sensor.consumption') | float) > 0 }}

This will be a sensor that tells you when production is greater than consumption. Then you can build an automation using it.

- alias: Notify when we have a power surplus for 30 minutes
  trigger:
    - platform: state
      entity_id: binary_sensor.surplus
      to: 'on'
      from: 'off'
      for:
        minutes: 30
  action:
    ... perform any action ...

Hi Petro,

exactly what i was thinking - you are spot on. Just as you were replying i made this sensor:

- platform: template
  sensors:
    netto_produktion:
      friendly_name: "Netto produktion af strøm"
      value_template: '{{ ( (states.sensor.smappee_home_solar.state | float ) - (states.sensor.smappee_home_active_power.state | float ) ) | round(1) }}'

The trick will be to get the automation not only to notify about power surplus, but to calculate the amount available.

Maybe it is possible to calculate the netto surplus for the last hour and display this as a number, eg: 0,52 kwh. I just don’t really know how to make such a sensor.

Thanks for your reply

/Peter

just another thought,

do have a ‘total for the day’ number for both production and consumption:

entity: smappee_solar_today
entity: smappee_power_today

the values are displayed as: eg 4.34 kwh for consumption and 6,33 for production

would it be possible to make a sensor that every minute calculates the theoretical surplus based on these two entities - for the last hour?

/Peter

Ouch! In the energy-rich part of the world where I live, electricity produced by hydro-electric generators costs about US 5.5 cents/kwh. There’s not much incentive for me to produce my own solar-electric power. Nevertheless, I appreciate your explanation of how it works in Denmark. The ‘one-hour storage’ concept is interesting.

ya, it used to be ‘one-year storage’ as an incentive (so you could ‘save up’ during summer). That was stopped a while back

Yes that should be possible with the statistics sensor

Make 2 sensors that only look at the past hour and then use a template to add or subtract the values.

1 Like

Hi again Petro,

that sounds right - but unfortunately that’s where my expertise comes to a holt.

Could you explain how you would go about that?

I have looked at the stat.sensor but I can’t figure out how to go about it. (I get the stat. part, it is the 2 sensors and how to set them up i don’t get)

Thanks for your input.

BR
Peter

Have you looked at the docs? There is an example directly in the documentation and each attribute is spelled out very clearly. Setting it up will be no different than the other sensors you’ve set up… it’s just different text that goes into your configuration.

ya, I looked at it and even replicated the two sensors in my setup.

Im afraid that I cannot get from the example to what I need. :confounded:

I’m not sure how you cant.

You want 1 hour in the past, and you need to match your sample size. You’ll have to calculate out your sample size. It depends on how many updates you get in an hour.

In this example, we have a new sample every minute, i.e 60 samples in an hour.

  - platform: statistics
    name: production 60
    entity_id: sensor.production
    samples: 60
    max_age:
      hour: 1
  - platform: statistics
    name: consumption 60
    entity_id: sensor.consumption
    samples: 60
    max_age:
      hour: 1

Then just subtract the 2 like before.

  - platform: template
    sensors:
      surplus:
        friendly_name: Power Surplus 60
        value_template: >
          {{ (states('sensor.production_60') | float - states('sensor.consumption_60') | float) }}
1 Like

thanks a lot for your reply, I appreciate you taking your time to help me.

Ill give it at go when i get home later today.

/Peter

Hi Petro… I am doing something very similar, but not for a specific time, so the stats sensor doesn’t fit my use case. I am wanting to calculate amount of energy I am sending back to the grid.
I have a sensor I created that is called net_energy, it is the sum of grid_power - solar_power
So I wanted to do something like this.
‘’’
If {
solar_power > 0
net_energy = net_energy + solar_power
‘’’
But I have no clue if I can do this in a value template.

Thanks!

yes you can but you also need to have an else statement. Otherwise what happens when solar_power is zero? what do you output?

Ok, I can add that, I am struggling with getting the syntax correct over all can you help me with that?

  - platform: template
    sensors:
      energy_to_grid:
        value_template: >-
        "{% if ((states('sensor.net_energy') | float) >= 0) %}
          {{ ((states('sensor.net_energy') | float) + (states('sensor.energy_to_grid') | float))  | round(1) }}
        {% endif %}"
        friendly_name: 'Energy to Grid'
        unit_of_measurement: 'W'     

remove the quotes around the whole template. The >- make it so you don’t need the quotes. Those symbols basically say “the next few lines are going to be a multi-line template”

I tried that, but I am getting this error.

while scanning for the next token
found character '%' that cannot start any token
  in "/config/configuration.yaml", line 616, column 10

well your indents are alittle uneven. make them flush and 2 spaces in from the edge of value_template.

Ok that now worked! Thanks for that help! However my logic is wrong! LOL

I shouldn’t be adding the two, I need to make one equal the other, I tried = to, but that didn’t work. Sorry I am used to coding in Java so this is a bit of a learning curve doing it this way.

hi all,

nice to see that there is acitivity on this topic. I must admit that I gave up a while back cos i did not have the time/kompetence to solve the probelm. Soon it will be sunny again and i am following this space with interest.

If you get something working please post it here so I can learn.

Thanks
br Peter