Combined sensor for P1 and P2 in energy dashboard

Hi,

Help or thoughts appreciated in my quest below:

For the energy dashboard I would like to show my separate P1 and P2 sensors (high/low tariff) combined into one “total” sensor. Both for solar panel production and energy consumption.
Currently the energy dashboard is showing to much bars to make it easy to read and have the WAF factor increase. And my wife should someday, somehow get exited about Home Assistant (so she knows what the heck I am fiddling around with all evening).
I have no interest whatsoever in the amount of energy used or the cost of it to have this separation in the energy dashboard. (there is an external app of the energy company providing that on a daily basis :grinning: )

Wtat I tried is to create template sensor which adds up the P1 and P2, so I thought it could then be added to the energy dashboard to replace both P1 and P2 sensors. For the sake of being correct on a daily basis I assume it is a good idea to have the template sensor set to 0 at 23:59:59 by means of an automation.

However, practice and theory differ as I am not able to choose them in the energy dashboard. What am I missing here?

The template sensors in configuration.yaml:

template:
  - sensor:
      - name: "Opbrengst panelen"
        unique_id: "opbrengst_panelen_totaal" 
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement
        state: >
            {{ states ('sensor.energy_production_tarif_1') | float(0) | round(0)
            + states ('sensor.energy_production_tarif_2') | float(0) | round(0)}}
        availability: >
            {{ states ('sensor.energy_production_tarif_1') | float(none) != none and
                states ('sensor.energy_production_tarif_2') | float(none) != none }}

      - name: "Afname stroom"
        unique_id: "afname_stroom_totaal" 
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement
        state: >
            {{ states ('sensor.energy_consumption_tarif_1') | float(0) | round(0)
            + states ('sensor.energy_consumption_tarif_2') | float(0) | round(0)}}
        availability: >
            {{ states ('sensor.energy_consumption_tarif_1') | float(none) != none and
                states ('sensor.energy_consumption_tarif_2') | float(none) != none }}
  1. Compare UOM from the source devices? Really W? Not Wh or kWh?

        unit_of_measurement: "kWh" # or Wh
        device_class: energy
        state_class: total_increasing
  1. Don’t reset at midnight. It is total incirising. Everything else is done by energy dashboard.

@arganto thanks for your answer!
Well watts or kWh or Wh does not make a difference in my example. Maybe that has to do with the use of power instead of energy device class?
I will try with your example.

What exactly do you mean with “compare unit of measurement”? Is there a flaw, did I miss something or is there something wrong I am not aware of? Let me know so I can improve.

I mean, that W and Wh ist a completely other dimension. If you have really W, what I don’t think, most probably you have to calculate integrate to Wh first. Important is the total increasing as well.

@arganto Thanks again!
All is kWh, thousand separator fooled me… learning on the fly :stuck_out_tongue:
So the new template sensors are in place and represent a value which seems ok. Update however is now over half an hour ago and no change whilst I really am consuming electricity. Is there a standard interval in updates, or should I set that in the template as well (state change)?

I used to use Domoticz for 4 years and knew it inside out, Home assistant is more user (instead of developer) friendly, but for me there is a lot to learn. So if questions seem obvious… then they are not obvious to me :grinning_face_with_smiling_eyes:

So the template sensors (without decimals) are red in the below picture, the 2 entries below each in green are the p1 and p2 readings from the smart meter. Now I’d like to have these sensors in the energy dashboard instead of the smart meter (green) ones. However despite the correct device class and UOM I am not able to select them. Am I missing something - again? Second picture is the energy dashboard setup. Netverbruik means consumption, zonne-energieproductie means solar production. In case you’re not a native Dutch…


Can your share your new template. With the mentioned parts, which I shared above, it should work as you want.

Sure! For the sake of completeness, the entire configuration.yaml. Anonimized ofcourse :grinning_face_with_smiling_eyes:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# google home
google_assistant:
  project_id: *******************
  service_account: !include ***************.json
  report_state: true

# Example configuration.yaml entry to enable the cloud component
cloud:

# Lampen in groepen
light:
  - platform: group
    name: Alle tuinverlichting
    entities:
      - light.afdak_1
      - light.afdak_2
      - light.afdak_3
      - light.afdak_4
      - light.afdak_5
      - light.boom_1
      - light.boom_2
      - light.boom_3
      - light.boom_4

# Sensoren
sensor:
  - platform: command_line
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    # If errors occur, make sure configuration file is encoded as UTF-8
    unit_of_measurement: "°C"
    value_template: "{{ value | multiply(0.001) | round(1) }}"
  - platform: afvalbeheer
    wastecollector: ROVA
    resources:
      - gft
      - papier
      - pmd
    postcode: ******
    streetnumber: ***
    dateformat: '%d %b'
    dateonly: 0
    nameprefix: 0
    builtinicons: 1
    dutch: 1

template:
  - sensor:
      - name: "Opbrengst panelen"
        unique_id: "opbrengst_panelen_totaal" 
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states ('sensor.energy_production_tarif_1') | float(0) | round(0)
            + states ('sensor.energy_production_tarif_2') | float(0) | round(0)}}
        availability: >
            {{ states ('sensor.energy_production_tarif_1') | float(none) != none and
                states ('sensor.energy_production_tarif_2') | float(none) != none }}

      - name: "Afname stroom"
        unique_id: "afname_stroom_totaal" 
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states ('sensor.energy_consumption_tarif_1') | float(0) | round(0)
            + states ('sensor.energy_consumption_tarif_2') | float(0) | round(0)}}
        availability: >
            {{ states ('sensor.energy_consumption_tarif_1') | float(none) != none and
                states ('sensor.energy_consumption_tarif_2') | float(none) != none }}

      - name: "Stroomverbruik netto"
        unique_id: "stroomverbruik_netto" 
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states ('sensor.energy_consumption_tarif_1') | float(0) | round(0)
            + states ('sensor.energy_consumption_tarif_2') | float(0) | round(0)
            - states ('sensor.energy_production_tarif_1') | float(0) | round(0)
            - states ('sensor.energy_production_tarif_2') | float(0) | round(0) }}
        availability: >
            {{ states ('sensor.energy_consumption_tarif_1') | float(none) != none and
                states ('sensor.energy_consumption_tarif_2') | float(none) != none and
                states ('sensor.energy_production_tarif_1') | float(none) != none and
                states ('sensor.energy_production_tarif_2') | float(none) != none }}

# Luxaflexen
cover:
  - platform: mqtt
    name: "Luxatest"
    command_topic: "Luxatest/blindsCommand"
    set_position_topic: "Luxatest/positionCommand"
    position_topic: "Luxatest/positionState"
    retain: true
    payload_open: "OPEN"
    payload_close: "CLOSE"
    payload_stop: "STOP"
    position_open: 0
    position_closed: 36
    
# heating
climate:
  - platform: generic_thermostat
    name: Badkamertest
    heater: switch.radiator_badkamer
    target_sensor: sensor.knmi_domoindepuntjes_gevoelstemperatuur
    min_temp: 10
    max_temp: 30
    ac_mode: false
    #target_temp: 5
    cold_tolerance: 0.5
    hot_tolerance: 0.0
    min_cycle_duration:
      seconds: 20
    initial_hvac_mode: "heat"
    precision: 0.5

I did it 1:1 Even with the non-existing entities (because of the correct 0 defaulting). And voilà

image

Perhaps, because you tried it first the other way and changes now? Please try with this example but with other name and other unique_id again. And don’t forget to reload templates or restart. And I would remove the availability as well.

It seems like that did the trick. Changing the name and unique_id solved the issue.
Thanks!!

Now give it some time to fill the dashboard…

Great to hear. Think there should have been an error message in developer tools → statistks as well (e.g. changing type/UOM) and a troubleshooting button. But here I prefer to start from scratch with a new name in such cases.

Yeah, that would be nice indeed.
Many thanks for all your help!

Running for 2 days now with the new sensors, happy! Only disadvantage is that historical data is not present in the energy dashboard anymore. Makes sense as the new sensors do not contain this data - although I hoped they did… I can “magically” make that appear by switching back to the old P1 and P2 sensors, however it is not very user friendly. Any ideas how to trick the older data into the new sensors?

Pics :grin: