Making Energy Panel work with Sense

I reported a bug, in a separate post, which turned out not to be a bug but needing some intervention. The “sense” integration provides sensors from Sense (Solar). The only sensors you could configure in the energy panel were things like “Daily from Grid” etc. Theoretically these work, but they only get updated once an hour (either Sense or the integration). These updates don’t always happen exactly on the hour and often are registered in the next hour. This causes corresponding energy bars to be shifted in the hr interval after the one they actually correspond to.

The solution I found is to create a few new template sensors using the provided sensor.energy_production and sensor.energy_usage, both in W and changing more frequently. It starts with:

- sensor:
  # Sensor that returns instantaneous power (W) taken from grid (>= 0), or put to grid (< 0)
  - name: net_power_from_grid
    unique_id: net_power_from_grid
    device_class: power
    unit_of_measurement: W
    availability: >-
      {{ true if states.sensor.energy_production and states.sensor.energy_usage }}
    state: >-
      {{ states.sensor.energy_usage.state | float - states.sensor.energy_production.state | float }}

This produces a more frequently update sensor for net_grid_power. From that we derive two others:

  # Sensor that returns instantaneous power (W) taken from grid (>= 0), or 0 if not taking
  - name: power_from_grid
    unique_id: power_from_grid
    device_class: power
    unit_of_measurement: W
    availability: >-
      {{ true if states.sensor.net_power_from_grid }}
    state: >-
      {{ max(states.sensor.net_power_from_grid.state | float, 0) }}

  # Sensor that returns instantaneous power (W) put to grid (>= 0), or 0 if not putting any
  - name: power_to_grid
    unique_id: power_to_grid
    device_class: power
    unit_of_measurement: W
    availability: >-
      {{ true if states.sensor.energy_production and states.sensor.energy_usage }}
    state: >-
      {{ max(-(states.sensor.net_power_from_grid.state | float), 0) }}

These separately track power to and from the grid, both expressed as positive values. Finally, define the following “integral” sensors to integrate power into energy (kWh):

- platform: integration
  name: home_energy_consumed
  source: sensor.energy_usage
  round: 5
  unit_prefix: k
  unit_time: h
  
- platform: integration
  name: solar_energy_produced
  source: sensor.energy_production
  round: 5
  unit_prefix: k
  unit_time: h
  
- platform: integration
  name: energy_from_grid
  source: sensor.power_from_grid
  round: 5
  unit_prefix: k
  unit_time: h
 
- platform: integration
  name: energy_to_grid
  source: sensor.power_to_grid
  round: 5
  unit_prefix: k
  unit_time: h

You now have all the sensors you need to properly configure the energy panel, but the sensors don’t show up there. You need some customizations that set things you cannot do directly in the above. Put these in customizations.yaml:

sensor.home_energy_consumed:
  device_class: energy
  friendly_name: Energy Consumed by Home
sensor.solar_energy_produced:
  device_class: energy
  friendly_name: Solar Energy Produced
sensor.energy_to_grid:
  device_class: energy
  friendly_name: Energy Sent to Grid
sensor.energy_from_grid:
  device_class: energy
  friendly_name: Consumed from Grid
sensor.power_to_grid:
  friendly_name: Power to Grid
sensor.power_from_grid:
  friendly_name: Power from Grid
sensor.net_power_from_grid:
  friendly_name: Net Power from Grid

So then, finally, you can:
CleanShot 2021-11-04 at 08.54.38
CleanShot 2021-11-04 at 08.54.50

The new sensors update much more frequently, and while there could be still one update that straddles the hour boundary, the difference is now minute and most everything shows up in the right 1 hr interval. Hope this helps somebody!

1 Like

Getting some yaml errors. What platform are these sensors on?

I was able to paste in the sensor examples above into a template.yaml file that I included from configuration.yaml as: template: !include template.yaml

I put the “platform: integration” examples into my sensor.yaml file includes as: sensor: !include sensor.yaml

I put the customizations into a customize.yaml included as: customize: !include customize.yaml