Creating a template energy sensor for energy dashboard

I extract my solar panel data from a rest interface (enphase envoy-s). Then I take that rest data and split it into different sensors using a template:

  - name: "Solar Today"
    state: "{% if state_attr('sensor.solar_production','wattHoursToday') != None %} {{ (states.sensor.solar_production.attributes['wattHoursToday'] | float / 1000 ) | round(1) }} {% endif %}"
    unit_of_measurement: "kWh"
    icon: mdi:solar-power
    state_class: measurement
    unique_id: "solar_today"
    device_class: "energy"
    #last_reset: '1970-01-01T00:00:00+00:00'

This does not show up in the energy dashaboard as selectable. I tried adding the last_reset but that is not allowed in a template. Any suggestions?

Add this to customize.yaml (or its equivalent via Configuration > Customizations):

sensor.solar_today:
  last_reset: '1970-01-01T00:00:00+00:00'

Reload Template Entities and it should appear in the dashboard.

NOTE
Customizations not only lets you modify an entity’s existing information, it also lets you add new, custom information, such as custom attributes (that would be rejected if you included them as part of the entity’s configuration).

1 Like

Thanks that works, now to see what happens when it goes past midnight (when the counter resets).

@123, could you tell if it still should work that way? Because it is not for me - I also tried adding a total_consumption attribute using your method. Still no success :frowning:

Although this thread is a year old, Manual Customization is still valid.

Oh, wow - I found out, what was going on! I forgot the device_class :blush:

Hi, I’ve been trying to create an entity to be used in energy dashboard, and apparently I’m following the rules and hints here above, but the entity doesn’t appear, even though I can see properly its value.
The sensor is reading, via rest, the value of exported energy.

This is the code:

  - platform: rest
    resource: http://192.168.1.165/metern/programs/programtotal.php    
    name: solar totali immissioni day
    value_template: '{{ value_json.Dailycounter4 }}'
    unit_of_measurement: KWh
    state_class: measurement
    device_class: energy
    unique_id: "immissioni_oggi"

This is what I added in customize.yaml:

sensor.solar_totali_immissioni_day:
  last_reset: '1970-01-01T00:00:00+00:00'

What am I missing?

Thanks!

1 Like

Same here guys, could really use some hints. My template sensor definition looks like this (which lives in a separate file for my templates called mytemplates.yaml):

  - unique_id: grid_import
    name: Grid Import
    unit_of_measurement: kWh
    state: >
      {% set production = states('sensor.growatt_total_energy_lifetime') | float(0) %}
      {% set consumption = states('sensor.growatt_local_load_total') | float(0) %}
      {% set import = consumption - production | float(0) %}
      {{ (import) | round(3) }}
    device_class: energy
    state_class: total_increasing
    icon: mdi:flash

Because I couldn’t select the sensor.grid_import in my energy dashboard (as grid input, duh), I added the following in my configuration.yaml, but still no cigar:

homeassistant:
  customize:
    sensor.grid_import:
      device_class: energy
      state_class: total_increasing
      last_reset: '1970-01-01T00:00:00+00:00'

Then I thought maybe the sensors I use in my template should also be marked as ‘energy’ (which shouldn’t be the case, right) so I added their definition here anyway, these are defined in an ESPHome yaml file:

sensor:
  - platform: modbus_controller
    name: "${devicename} Total Energy Lifetime"
    address: 55
    register_type: "read"
    unit_of_measurement: kWh
    state_class: total_increasing
    device_class: energy
    icon: mdi:flash
    value_type: U_DWORD
    accuracy_decimals: 1
    filters:
    - multiply: 0.1
  - platform: modbus_controller
    name: "${devicename} Local Load Total"
    address: 1062
    register_type: "read"
    unit_of_measurement: kWh
    state_class: total_increasing
    device_class: energy
    icon: mdi:flash
    value_type: U_DWORD
    accuracy_decimals: 1
    filters:
    - multiply: 0.1

Any help/hints/etc. highly appreciated!
Thanks!

EDIT: I added the sensor manually to the energy file in /config/.storage/energy and rebooted my device. That does seem to work (at least I see the data in my dashboard now), however I still can’t select that sensor from the dropdown in the UI (Settings → Dashboards → Energy)
So for me at least right now I have a working setup :+1:

EDIT2: ignore that, the grid import shows up in the energy dashboard, but it remains at 0kWh, so it doesn’t work after all.

Small update, under ‘Developer Tools → Statistics’ I was able to find an issue with my template sensor, it stated that the unit changed from kW to kWh (which was likely a copy-paste error when I first created the sensor). I clicked ‘fix it’ and chose to delete all previous data. After this, I was able to select my template sensor in the Energy dashboard settings. I’ll update you if I see actual data coming in.

PS: a warning icon or something would be nice in the dropdown instead of just not showing the sensor :slight_smile:

1 Like