Peak and offpeak tariffs - need to setup multiple tariff changes per day

Hi guys,
I am struggling very hard to implement peak and offpeak tariff and the prices in Home Assistant.
Went through many topics, tried everything possible I found, but no success.

The thing is, that I need to setup the OFF-PEAK tariff like that (and also I need to change the times like 2-3 time per year depending on the electricity provider who changes the peak - offpeak times):

Working days
00:00:00 - 07:15:00
08:15:00 - 10:15:00
11:15:00 - 13:45:00
14:45:00 - 16:45:00
17:45:00 - 23:59:00

Then it means, that the PEAK tariff will be:
07:15:00 - 08:15:00
10:15:00 - 11:15:00
13:45:00 - 14:45:00
16:45:00 - 17:45:00

Weekends OFF-PEAK tariff

00:00:00 - 08:15:00
09:15:00 - 11:15:00
12:15:00 - 14:15:00
15:15:00 - 17:15:00
18:15:00 - 23:59:00

So again - the PEAK tariff is:
08:15:00 - 09:15:00
11:15:00 - 12:15:00
14:15:00 - 15:15:00
17:15:00 - 18:15:00

Means that I need the PEAK and OFF-PEAK tariffs to change based on the day and time as described above. In some of the last HA versions I was able to create a daily schedule which can held more times than one. Now it was replaced by the “graphical” schedule and this only works with half hours (30 minutes changes).

Can some of you guys guide me, how to do that? This is one of the last important things I need to have working in HA.

Thanks very much

Wow that’s quite a few peak and off peak changes whys it so many ?
I’m guessing you have your energy sensor already and if so have your created the peak and off peak sensors using the utility meter integration.

This template gives true if you’re on peak rates. You’ll need the workday integration and time sensor set up, and you must be on 2022.7 or later for the bool function. Time format is critical in the lists: don’t deviate from hh:mm-hh:mm:

{% set pk = ['07:15-08:15',
             '10:15-11:15',
             '13:45-14:45',
             '16:45-17:45']

   if bool(states('binary_sensor.workday_sensor')) else

            ['08:15-09:15',
             '11:15-12:15',
             '14:15-15:15',
             '17:15-18:15'] %}

{% set tm = states('sensor.time') %}
{{ bool((pk|select('<=',tm~'.'))|map('reverse')|map('truncate',length=5,end='')|map('reverse')|select('>',tm)|list|count) }}
1 Like

Hej man, nice to see you again :-).
Just tried to copy your code and make the template, but it shows me this error.
I am sure I am doing something wrong…

And after I succeed with this template, I would need to define the peak and offpeak prices.
This is also done in configuration.yaml?

Thanks

Well this is just how it is. EON in Czech depending on your tariff makes 20 hours a day off-peak and 4 hours a day peak. And of course, to be more complex, different times on weekends.
That´s why I am desperate at setting this up…
Just talking to Troon and hopefully we will get the solution.

Do you have the workday integration and the time sensor installed? What does this give?

{{ states('binary_sensor.workday_sensor') }}
{{ states('sensor.time') }}

Yes, once it’s working it’d need to go in your config somewhere like this:

template:
  - binary_sensor:
      - name: "Peak tariff"
        state: >-
          {% set pk = ['07:15-08:15',
                       '10:15-11:15',
                       '13:45-14:45',
                       '16:45-17:45']

             if bool(states('binary_sensor.workday_sensor')) else

                      ['08:15-09:15',
                       '11:15-12:15',
                       '14:15-15:15',
                       '17:15-18:15'] %}

          {% set tm = states('sensor.time') %}
          {{ bool((pk|select('<=',tm~'.'))|map('reverse')|map('truncate',length=5,end='')|map('reverse')|select('>',tm)|list|count) }}

You could define the times via input_text or input_datetime fields but let’s get this bit working first.

1 Like

I done that via your guide, checked the config, restarted without issues, but can not see the workday sensor. Using the 2023.1.7 version.

Don´t know, what could be wrong. Should I specify more precisely the working days, like that for example:

# Example 1 configuration.yaml entry
binary_sensor:
  - platform: workday
    country: CZ
    workdays: [mon, tue, wed, thu, fri]
    excludes: [sat, sun]

Update - sensor time and date is working properly (as far as I can see).
Workday still not…

That is odd. Is that the only binary_sensor: in your config file? No !include statements?

Template working fine here at 14:12:

1 Like

You have to restart HA after adding yaml integrations.

Whilst you try to get it working, you could write your own and substitute its name into the tariff template:

template:
  - binary_sensor:
      - name: "Temp workday sensor"
        state: "{{ now().weekday() < 5 }}"
 
      - name: "Peak tariff"
# etc
1 Like

Done - I had to put the workday binary sensor under the meteoalarm binary sensor I already have in the config. Didn´t know they have to be each below other.

Template working fine as well now :slight_smile:

You should only have one top-level header for each domain, so:

binary_sensor:
  - platform: meteoalarm
    ...
  - platform: workday
    country: CZ

Glad it’s working — it’s a template I’m particularly proud of.

1 Like

For sure, it´s very nice.

So now I have in the config this code:

Or shall I use the other one?

{% set pk = ['07:15-08:15',
             '10:15-11:15',
             '13:45-14:45',
             '16:45-17:45']

   if bool(states('binary_sensor.workday_sensor')) else

            ['08:15-09:15',
             '11:15-12:15',
             '14:15-15:15',
             '17:15-18:15'] %}

{% set tm = states('sensor.time') %}
{{ bool((pk|select('<=',tm~'.'))|map('reverse')|map('truncate',length=5,end='')|map('reverse')|select('>',tm)|list|count) }}
1 Like

They’re the same…? The screenshot shows the template embedded in the sensor, which is what you need. That’ll give you binary_sensor.peak_tariff which will be 'on' at peak time, 'off' otherwise.

1 Like

RIght, I´ve checked now and it´s working.
For example now 16:45 - 17:45 the peak tariff is active - and shown properly as the entity.

So what shall we do next - how to setup the prices for the peak and offpeak?

Peak = 3,24802 CZK / 1 kWh
Offpeak = 2,64396 CZK / 1kWh

And there is also a regular monthly fee paid for the electricity service 488,03 CZK per month.
If it´s possible to somehow add this to every month cost, that would be fine.
But if not or if it´s diffucult, no need to have that.

Under your existing template: block, indented the same as your first binary sensor:

  - sensor:
      - name: "Electricity tariff"
        unit_of_measurement: 'CZK/kWh'
        state: "{{ 3.24802 if bool(states('binary_sensor.peak_tariff')) else 2.64396 }}"

If you have a measurement of your power consumption, you can set up your energy dashboard and include this tariff sensor:

1 Like

YES, YES and YES. I made a setup of the prices in the electricity dashboard and seems now everything will start to calculate. Will wait till the evening or tomorrow to see the prices.

For now I would like very much to thank you Troon - solution marked and hearts given!!!

And I have just learned a lot studying the codes and way how to do things…

1 Like

That template is probably not a good place to start learning :smiley:. If you can describe, step-by-step, how it calculates whether you’re in a peak period I’ll be very impressed.

I´ll be watching the progress in the dashboard and will let you know, how it works :slight_smile: