Configuring Utility Meters for 64 Smarthome sensors, need help :)

I have the following challenge for configuring Utility Meter

  • 64 smart meter sensors that measure power consumption.
  • 2 electricity tariffs (high and low tariff)

I would like to present the following data:

  • Sensor consumption per day, week, month, year
  • Comparison consumption sensor previous / current day, weeks, month, year
  • Cost sensor per day, week, month, year
  • Compare costs of previous / current day, weeks, month, years

If I did this without scripting with YAML, it would only work with many sensor templates and would not be very clear.

How can I solve this generically so that I only have one processing with all sensors and so that the results can be obtained per sensor.

Example for a sensor (total 254):

utility_meter:
  ## #######################################################
  ## track consumptions energy house
  ## #######################################################
  verbrauch_tag:
    source: sensor.energieverbrauch_gesamt
    cycle: daily
    tariffs:
      - ht
      - nt
 ... more 254 entries ??? 

Calculation parameters

## ################################################
##  input number tariff
## ################################################
input_number:
  # Hochtarif
  preis_ht:
    name: Preis Tagesstrom
    min: 0.001
    max: 0.500
    unit_of_measurement: "€/kWh"
    initial: 0.12606
    mode: box
    icon: mdi:currency-eur

  ## Niedertarif
  preis_nt:
    name: Preis Nachtstrom
    min: 0.001
    max: 0.500
    unit_of_measurement: "€/kWh"
    initial: 0.09696
    mode: box
    icon: mdi:currency-eur

## ################################################
##  input date time for NT / HT / Weekend
## ################################################
input_datetime:
  # Start Hochtarif
  ht_start_zeit:
    name: "Zeit Tagstrom"
    has_date: false
    has_time: true
    initial: "06:00"

  # Start Niedertarif
  nt_start_zeit:
    name: "Zeit Nachstrom"
    has_date: false
    has_time: true
    initial: "22:00"

  # Start Niedertarif Samstag
  we_start_zeit:
    name: "Zeit Niedertarif"
    has_date: false
    has_time: true
    initial: "13:00"

binary_sensor:
  - platform: workday
    name: wochentag
    country: AT

Automation switch tariff

## #################################################
##  select tariff based on the settings
## #################################################

##  ENERGY Tagstrom HT 
- id: energy_haus_ht
  alias: Energie Haus Hocharif
  initial_state: on
  trigger:
    - platform: state
      entity_id: sensor.tarif_ht_on
      to: 'on'
  action:
    - service: utility_meter.select_tariff
      data:
        entity_id: 
          - utility_meter.verbrauch_tag
          - utility_meter.verbrauch_monat
          ... more 256 entities
        tariff: ht
   
##  ENERGY Nachtstrom NT 
- id: energy_haus_nt
  alias: Energie Haus Niedertarif
  initial_state: on
  trigger:
    - platform: state
      entity_id: sensor.tarif_ht_on
      to: 'off'
  action:
    - service: utility_meter.select_tariff
      data:
        entity_id: 
          - utility_meter.verbrauch_tag
          - utility_meter.verbrauch_monat
          ... more 256 entities
        tariff: nt

Calculation consumtion and costs for 1024 sensors

## ################################################
##  Sensors based on utility_meter
## ################################################
sensor:
  - platform: template
    sensors:
      ## ---------------------------------------------------------
      ## d0: current timestamp
      ## d1: start time HT, every day at 06:00
      ## d2: end time HT, every day at 22:00 or saturday at 13:00
      ## ---------------------------------------------------------
      tarif_ht_on:
        friendly_name: "Tarif HT"
        entity_id: sensor.time
        icon_template: mdi:calendar-range
        value_template: >-
          {% set t=now() %}    
          {% set d0=as_timestamp(t) %}
          {% set d1=as_timestamp(as_timestamp(t)|timestamp_custom("%Y-%m-%d 06:00")) %}
          {% if (as_timestamp(t)|timestamp_custom('%a')=="Sat") %}
              {% set d2=as_timestamp(as_timestamp(t)|timestamp_custom("%Y-%m-%d 13:00")) %}
          {% else %}
              {% set d2=as_timestamp(as_timestamp(t)|timestamp_custom("%Y-%m-%d 22:00")) %}
          {%endif%}
          {% if (d0>=d1) and  (d0<=d2) %}on{% else %}off{% endif %}
      ## energy sensors
      energy_total_dayusage:
        friendly_name: Verbrauch Tag gesamt
        entity_id:
          - sensor.verbrauch_tag_ht
          - sensor.verbrauch_tag_nt
        icon_template: mdi:chart-histogram
        unit_of_measurement: kWh
        value_template: >-
          {{ 
            ((states.sensor.verbrauch_tag_ht.state | float) +
            (states.sensor.verbrauch_tag_nt.state | float)) | round(3)
          }}

      energy_costs_perday:
        friendly_name: Energiekosten/Tag
        entity_id:
          - sensor.verbrauch_tag_ht
          - sensor.verbrauch_tag_nt
        unit_of_measurement: "€"
        icon_template: mdi:currency-eur
        value_template: >-
          {{ 
            (((states.sensor.verbrauch_tag_ht.state | float) * (states.input_number.preis_ht.state| float)) +
            ((states.sensor.verbrauch_tag_nt.state | float) * (states.input_number.preis_ht.state| float))) | round(2)
          }}

     ... more 1022 entities

I’m not sure if I understand the actual question. I have a sensor for every single utility meter and 1 sensor that adds them all together, which I think is the part you want to template?

I dump it all into influxDB and for daily/weekly etc usage I simply do database queries like this:

    - name: Average Power (last hour)
      database: homeassistant
      where: '"entity_id" = ''energy_combined_power'' AND time >= now() - 1h'
      measurement: '"W"'
      value_template: "{{ value | round(2) }}"
      unit_of_measurement: W
    - name: Average Power (last day)
      database: homeassistant
      where: '"entity_id" = ''energy_combined_power'' AND time >= now() - 24h'
      measurement: '"W"'
      value_template: "{{ value | round(2) }}"
      unit_of_measurement: W
    - name: Average Power (last week)
      database: homeassistant
      where: '"entity_id" = ''energy_combined_power'' AND time >= now() - 7d'
      measurement: '"W"'
      value_template: "{{ value | round(2) }}"
      unit_of_measurement: W

If you want to combine your sensors with a template you could loop around the sensor domain, maybe? I’m not 100% sure tho

- platform: template
  sensors:
    all_meters:
      friendly_name: "All Meters"
      value_template: >
        {%- set domains = ['sensors'] -%}
        {%- for domain in domains -%}
          ...
        {%- endfor -%}

Thank you for the information. But I think it will not be possible to use “utility_meter” to calculate the “Day, Month, Year” periods for each tariff and the costs for each of these sensor values. It would only be possible generically with a script. But I have no idea how to do that.

all sensors

Sensor State
sensor.verbrauch_bad 20.0
sensor.verbrauch_buro 360.0
sensor.verbrauch_dachgeschoss 10.0
sensor.verbrauch_dachgeschoss 0.29
sensor.verbrauch_dg_fitnessstudio 0.29
sensor.verbrauch_eg_buro 2.56
sensor.verbrauch_eg_gang 0.45
sensor.verbrauch_eg_gargage 0.64
sensor.verbrauch_eg_heizung 0.00
sensor.verbrauch_eg_vorratsraum 2.52
sensor.verbrauch_eg_waschraum 15.75
sensor.verbrauch_eg_zschrank 0.43
sensor.verbrauch_ergeschoss 470.0
sensor.verbrauch_fitnessstudio 10.0
sensor.verbrauch_gang_eg 0.0
sensor.verbrauch_gang_og 0.0
sensor.verbrauch_gang_wg 0.0
sensor.verbrauch_garage 0.0
sensor.verbrauch_gesamt 630.0
sensor.verbrauch_haus 0.00
sensor.verbrauch_heizung 0.0
sensor.verbrauch_kinderzimmer 10.0
sensor.verbrauch_kuche 100.0
sensor.verbrauch_monat 53.20
sensor.verbrauch_obergeschoss 30.0
sensor.verbrauch_obergeschoss 3.37
sensor.verbrauch_og_bad 1.10
sensor.verbrauch_og_gang 2.08
sensor.verbrauch_og_kinderzimmer 0.26
sensor.verbrauch_og_schlafzimmer 0
sensor.verbrauch_reserve 0.0
sensor.verbrauch_rolladen_kuche 261.3
sensor.verbrauch_rolladen_nord 343.5
sensor.verbrauch_rolladen_sud 275.90
sensor.verbrauch_rsg 0.0
sensor.verbrauch_schlafzimmer 0.0
sensor.verbrauch_unbekannt 0.0
sensor.verbrauch_vorratsraum 20.0
sensor.verbrauch_waschraum 80.0
sensor.verbrauch_wg_gang 0.07
sensor.verbrauch_wg_kuche 8.43
sensor.verbrauch_wg_wohnzimmer 0.38
sensor.verbrauch_wohngeschoss 120.0
sensor.verbrauch_wohnzimmer 20.0
sensor.verbrauch_zahlerschrank 10.0
sensor.smartmeter_gesamt 17582.4
sensor.smartmeter_s01 100
sensor.smartmeter_s02 0
sensor.smartmeter_s03 0
sensor.smartmeter_s04 560
sensor.smartmeter_s05 2230
sensor.smartmeter_s06 460
sensor.smartmeter_s07 70
sensor.smartmeter_s08 3570
sensor.smartmeter_s09 0
sensor.smartmeter_s10 80
sensor.smartmeter_s11 310
sensor.smartmeter_s12 710
sensor.smartmeter_s13 1230
sensor.smartmeter_s14 0
sensor.smartmeter_s15 0
sensor.smartmeter_s16 2080
sensor.smartmeter_s17 0
sensor.smartmeter_s18 40
sensor.smartmeter_s19 0
sensor.smartmeter_s20 0
sensor.smartmeter_s21 0
sensor.smartmeter_s22 0
sensor.smartmeter_s23 220
sensor.smartmeter_s24 0
sensor.smartmeter_s25 220
sensor.smartmeter_s26 100
sensor.smartmeter_s27 0
sensor.smartmeter_s28 0
sensor.smartmeter_s29 650
sensor.smartmeter_s30 610
sensor.smartmeter_s31 0
sensor.smartmeter_s32 2340
sensor.smartmeter_s33 1100
sensor.smartmeter_s34 14500
sensor.smartmeter_s35 4400
sensor.smartmeter_s36 4410
sensor.smartmeter_s37 440