Change tariffs based on DST

I have the following automation that changes tariffs based on time but because of DST I need to keep updating the times.

- alias: Energy - Switch tariff
  description: ''
  trigger:
  - platform: time
    at: 08:15:00
    variables:
      tariff: peak
  - platform: time
    at: '23:15:00'
    variables:
      tariff: offpeak
  condition: []
  action:
  - service: select.select_option
    target:
      entity_id: select.daily_energy
    data:
      option: '{{ tariff }}'
  - service: select.select_option
    target:
      entity_id: select.monthly_energy
    data:
      option: '{{ tariff }}'
  - service: select.select_option
    target:
      entity_id: select.yearly_energy
    data:
      option: '{{ tariff }}'

Is there a way to update the yaml to have this automatically change with DST - it will need to change between these values and 09:15:00 - 00:15:00

Maybe a toggle helper and when it is on, then add time to your automation times.

{% if binary_sensor.dst %}
	'00:15:00'
{% else %}
	'23:15:00'
{% endif %}

You could then make an automation to set that to on and off on certain days and times, if you are too lazy to do it yourself :stuck_out_tongue:

Mine:

template:
  - binary_sensor:
      - name: "Peak Rate"
        icon: "mdi:power-plug"
        state: >
          {% if states('binary_sensor.is_dst')|bool(0) %}
            {{ (now().weekday() < 5) and ( (7 < now().hour < 11) or (16 < now().hour < 22) ) }}
          {% else %}
            {{ (now().weekday() < 5) and ( (6 < now().hour < 10) or (15 < now().hour < 21) ) }}
          {% endif %}

You call this binary sensor inside the automation? And I need to create a bool called is_dst that I need another automation to toggle it on/off?

Yes, I use it to trigger the switching of peak/off-peak tariffs of my utility meter:

- id: 841ae395-4646-4c95-8991-14cdd7e9c834
  alias: 'Set Peak or Off-peak Tariff'
  trigger:
  - platform: state
    entity_id: binary_sensor.peak_rate
  action:
  - service: select.select_option
    target:
      entity_id:
      - select.energy_upstairs_heat_pump_daily
      - select.energy_hot_water_daily
      - select.energy_from_grid_daily
    data:
      option: "{{ 'peak' if is_state('binary_sensor.peak_rate', 'on') else 'offpeak' }}"

Oh yeah. I missed that, sorry:

template:
  - binary_sensor:
      - name: "Is DST"
        state: "{{ now().timetuple().tm_isdst == 1 }}"

Thanks.
now().timetuple().tm_isdstIs this a HA built-in function that determines DST based on timezone of the HA server?

Correct.

1 Like

Sweet, and can that be somehow put into the automation directly without creating the two extra sensors?

Sure you could put all the templates in a trigger.

The DST template only changes twice a year so you still have to choose your peak/off-peak times based on this.

Nice, never done a trigger like that. How would we add it there?

https://www.home-assistant.io/docs/automation/trigger/#template-trigger

Ok had a look there but no idea how to add all that data to the one automation so might just do it the way you have it.
Question on this (now().weekday() < 5) is that a Monday to Friday thing?

Yes it is. Weekends are off-peak here.

I think I have mine like this:

{% if states('binary_sensor.is_dst')|bool(0) %}
{{((now().hour >= 0 and now().minute >=15)) and (now().hour <= 9 and now().minute <= 15 )}}
{% else %}
{{((now().hour >= 23 and now().minute >=15)) and (now().hour <= 8 and now().minute <= 15 )}}
{% endif %}

For 00:15 to 9:15 and then for 11:15 to 8:15 but they both show as false in the template editor, probably due to the minutes, can’t be both less than or greater than 15.
Any ideas?

Found this {{ now() >= today_at("00:15") and now() <= today_at("09:15") }} which of course doesn’t work for {{ now() >= today_at("23:15") and now() <= today_at("08:15") }}

Try it like this:

template:
  - binary_sensor:
      - name: "Peak Rate"
        icon: "mdi:power-plug"
        state: >
          {% if states('binary_sensor.is_dst')|bool(0) %}
            {% set t_on = today_at('00:15') %}
            {% set t_off = today_at('09:15') %}
          {% else %}
            {% set t_on = today_at('11:15') %}
            {% set t_off = today_at('20:15') %}
          {% endif %}
          {% set t_now = now() %}
          {{ t_on <= t_now <= t_off }}

Don’t forget to create binary_sensor.is_dst.

Ok so I have these now:
image
image

Something not right as peak right should be on right now (09:32 local)?

- binary_sensor:
  - name: "Is DST"
    state: "{{ now().timetuple().tm_isdst == 1 }}"
  - name: "Peak Rate"
    icon: "mdi:power-plug"
    state: >
      {% if states('binary_sensor.is_dst')|bool(0) %}
        {% set t_on = today_at('09:15') %}
        {% set t_off = today_at('00:15') %}
      {% else %}
        {% set t_on = today_at('08:15') %}
        {% set t_off = today_at('23:15') %}
      {% endif %}
      {% set t_now = now() %}
      {{ t_on <= t_now <= t_off }}

Problem is this again {% set t_off = today_at('00:15') %} same as my code, this is technically tomorrow so it’s not right.
The else works right as both times are within the same day

During summer time, what time dose peak time start. What time does it stop?
And likewise for non DST.

Summer time peak is 09:00 - 00:00 and winter is 08:00 - 23:00 but I need to add the 15 mins because my meter’s clock is 15 minutes off hence the 15.
The logic above works fine for DST if I do this:

{% if states('binary_sensor.is_dst')|bool(1) %}
        {% set t_on = today_at('08:15') %}
        {% set t_off = today_at('23:15') %}
      {% else %}
        {% set t_on = today_at('09:15') %}
        {% set t_off = today_at('00:15') %}
      {% endif %}
      {% set t_now = now() %}
      {{ t_on <= t_now <= t_off }}

but not for non-DST.