Power-based tariff

Hi,

I am trying to bend my head aroud how to make a sensor to track the electricity cost, as the delivering company plans to switch from a time based tariff to a power based one.
Today there are two tariffs, high and low based on time of date and year. This was simple to implement with tariff changing automation.

The soon-to-come power based tariff is a bit more complicated for me in HA, as it is based on the average of the period’s (calendar month) three peaks for 1h power consumption (kW) per day.

Ie the cost is calculated as an average of those three readings, multiplied with a constant ($/kW) set by the electicity company.

For any given month the sensor will need to capture the three topmost power readings from the smart meter - or as it seems now, from Influx.
Keeping track of one max should be fairly easy, but keeping track of highest, second highest and third highest feels a bit trickier.

Any suggestion is welcome :slight_smile:

First get the 3 sensors into home assistant. Second make 3 template sensors that sort the data and update appropriately.

Here’s the template sensors

template:
- trigger:
  - platform: state
    entity_id: &unsorted_sensors
    - sensor.1
    - sensor.2
    - sensor.3
    not_to:
    - unknown
    - unavailable
    variables: &sorted_variables
      sensors: *unsorted_sensors
      values: >
        {{ sensors | map('states') | map('float', 0) | sort(reverse=True) }}
  - platform: homeassistant
    event: start
    variables: *sorted_variables
  sensor:
  - name: Highest Tariff
    unique_id: highest_tariff
    state: "{{ values[0] }}"
  - name: Middle Tariff
    unique_id: middle_tariff
    state: "{{ values[1] }}"
  - name: Lowest Tariff
    unique_id: lowest_tariff
    state: "{{ values[2] }}"
1 Like

Thank you for the nudge.

As often happens when my own knowledge runs out and I give a shout here, there’s something entirely new for me - this time the usage of & and * in references. I found it used in a few posting here but not enough to actually grasp its usage, can you please point me to the valkd documentation, or elaborate on it here.

They are yaml anchors

& declares the anchor, * uses it.

So unsorted_sensors is basically passing the list of sensors to variables.sensors and sorted_variables is being passed to variables in the other trigger.

So you only have to manage 1 list of sensors and 1 variable section. Otherwise you’d have to copy/paste it multiple times.

Thank you, again!

This should be useful in some of my other declarations too.