Please help me create a sensor that keeps adding values for a day to itself

Hi All, HA newbie here and first time poster.

My home energy monitoring is currently uploaded to PVOutput. I have 4 values I can use:

  • Energy generated
  • Energy Consumed
  • Power created (in 5 min increments)
  • Power used (in 5 min increments)

What PVOutput calculates (but you can’t import into HA via API) is how much of my solar I used (over any 5 min period) from what is generated and how much is exported.

I’ve managed to make some sensors (based on power) that follow the logic:
If Solar generated > power consumed:
Solar used by house = power consumed
Solar exported to grid = Solar generated - power consumed
If Solar generated < Power consumed
Solar used by house = Solar generated
Solar exported to grid = 0
Power consumed from grid = Power used - Solar Generated

image

- sensor:
    - name: Daily_Solar_exported_to_home
      unit_of_measurement: Wh
      device_class: energy
      state_class: total
      state:
        "{% if (states('sensor.macca_s_solar_power_consumed') |float) > (states('sensor.macca_s_solar_power_generated') |float) %}
          {{(states('sensor.macca_s_solar_power_generated') | float |abs | round(0)) }}
        {% else %}
          {{(states('sensor.macca_s_solar_power_consumed') |float |abs | round(0)) }}
        {% endif %}"

    - name: Daily_Solar_exported_to_grid
      unit_of_measurement: Wh
      device_class: energy
      state_class: total
      state:
        "{% if (states('sensor.macca_s_solar_power_consumed') |float) < (states('sensor.macca_s_solar_power_generated') |float) %}
          {{ (states('sensor.macca_s_solar_power_generated') | float |abs | round(0)) - (states('sensor.macca_s_solar_power_consumed') | float |abs | round(0)) }}
        {% else %}
          {{ 0 }}
        {% endif %}"

    - name: Daily_Energy_imported_from_grid
      unit_of_measurement: Wh
      device_class: energy
      state_class: total
      state: >
        {% if (states('sensor.macca_s_solar_power_consumed') |float) > (states('sensor.macca_s_solar_power_generated') |float) %}
          {{ (states('sensor.macca_s_solar_power_consumed') | float |abs | round(0)) - (states('sensor.macca_s_solar_power_generated') | float |abs | round(0)) }}
        {% else %}
          0
        {% endif %}
    - name: Solar_Power_exported
      unit_of_measurement: Wh
      device_class: energy
      state_class: total_increasing
      state: >
        {% if (states('sensor.macca_s_solar_power_consumed') |float) < (states('sensor.macca_s_solar_power_generated') |float) %}
          {{ (states('sensor.macca_s_solar_power_generated') | float |abs | round(0)) - (states('sensor.macca_s_solar_power_consumed') | float |abs | round(0)) /1000 }}
        {% else %}
          0
        {% endif %}

    - name: Solar_power_used_in_home
      unit_of_measurement: W
      device_class: energy
      state: >
        {% if (states('sensor.macca_s_solar_power_consumed') |float) > (states('sensor.macca_s_solar_power_generated') |float) %}
          {{(states('sensor.macca_s_solar_power_generated') | float |abs | round(0)) }}
        {% else %}
          {{(states('sensor.macca_s_solar_power_generated') | float |abs | round(0)) - (states('sensor.macca_s_solar_power_consumed') |float |abs | round(0))  }}
        {% endif %}

    - name: Net_Solar_Power
      unit_of_measurement: W
      device_class: energy
      state: >
        {{ ((states('sensor.macca_s_solar_power_generated') |float | abs) - (states('sensor.macca_s_solar_power_consumed') | float | abs)|float |abs | round(0)) }}

How do I create a sensors (assuming this is what I want to add to the energy cards) (measured in kW, total_increasing) that checks the values above in 5 minute increments, starting at 12:02am and resets at 12:00am every day? I would like 3 values please:

  • Solar exported to grid
  • Solar used by home
  • Energy from grid

And also, is it possible to put tariff times and costs in the sensors or do I configure this somewhere else?

Thanks in Advance.

The utility meter helper can aggregate energy sensors for a daily reset cycle:

Thanks @tom_l - I did see this but couldn’t see anywhere how it could split the solar I generate into “consumed by house” and “exported to grid”

That is what template sensors are for. Then feed those to the utility meter.

Also you don’t need to do this if you are using the built in energy dashboard. All you need is:

Grid Consumption:
Should be an energy sensor that is always positive that only shows how much energy you used from the grid. It should not include solar consumption.

Return to Grid:
Should be an energy sensor that is always positive that only shows how much you exported to the grid.

Solar Production:
Should be an energy sensor that is always positive that only shows how much solar energy you produced.

Thanks - my issue is the “data” I have doesn’t split between Solar produced and what portion of it is exported to grid (so it assumes the entire production is exported (at one rate) and I import my entire consumption (at a different rate).
I can calculate how much I use and how much I export - but I don’t know how to create “something” (I am assuming a sensor so I can use in the in the Energy card)

Thanks

This is what today (up until now) looks like - 100% exported when in fact I have consumed much of what I have generated)

Since about 8:45am I have produced all I have consumed and managed to export to difference (will post another screenshot - just need to wait as a new user)

Use template sensors to calculate the required sensors.

What energy sensors do you have available?

List their entity ids and what they report. Then I can help you with the template sensors.

Do this as text, not a screen shot.

From PVOutput I can ingest the highlighted ones

and a link to PvOutput for my system → Macca's Solar 5.250kW

What did I say?

Also that screenshot means nothing to me without

It also includes a lot of power sensors when I specifically asked you to supply:

Sorry - missed the txt bit. Is this what you mean?

sensor.daily_energy_imported_from_grid
Daily_Energy_imported_from_grid
0	state_class: total
unit_of_measurement: Wh
device_class: energy
friendly_name: Daily_Energy_imported_from_grid
sensor.energy_current_hour
Solar production forecast Estimated energy production - this hour
0.699	unit_of_measurement: kWh
device_class: energy
friendly_name: Solar production forecast Estimated energy production - this hour
sensor.energy_next_hour
Solar production forecast Estimated energy production - next hour
0.937	unit_of_measurement: kWh
device_class: energy
friendly_name: Solar production forecast Estimated energy production - next hour
sensor.energy_production_today
Solar production forecast Estimated energy production - today
8.638	unit_of_measurement: kWh
device_class: energy
friendly_name: Solar production forecast Estimated energy production - today
sensor.energy_production_today_remaining
Solar production forecast Estimated energy production - remaining today
7.389	unit_of_measurement: kWh
device_class: energy
friendly_name: Solar production forecast Estimated energy production - remaining today
sensor.energy_production_tomorrow
Solar production forecast Estimated energy production - tomorrow
7.158	unit_of_measurement: kWh
device_class: energy
friendly_name: Solar production forecast Estimated energy production - tomorrow
sensor.macca_s_solar_energy_consumed
Macca's Solar Energy consumed
12909.0	state_class: total_increasing
unit_of_measurement: Wh
device_class: energy
friendly_name: Macca's Solar Energy consumed
sensor.macca_s_solar_energy_generated
Macca's Solar Energy generated
7634.0	state_class: total_increasing
unit_of_measurement: Wh
device_class: energy
friendly_name: Macca's Solar Energy generated

I am not sure if this is what you want and the format? The above is the txt of this

Thanks

Close enough.

Restart home assistant after creating this:

Located in configuration.yaml:

template:
  - sensor:
      - name: Return to Grid
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: Wh
        state: "{{ states('sensor.macca_s_solar_energy_generated')|float - states('sensor.macca_s_solar_energy_consumed')|float }}"
        availability: "{{ has_value('sensor.macca_s_solar_energy_generated') and has_value('sensor.macca_s_solar_energy_consumed') }}"

Then set your energy dashboard up as follows:

Grid Consumption:

sensor.daily_energy_imported_from_grid

Return to Grid:

sensor.return_to_grid

Solar Production:

sensor.macca_s_solar_energy_generated

I’ll try that as soon as I get a chance - thanks so much.

I’ll report back