Issue with Template Trigger

I’m on tariff with my electricity provider which has ‘peak hour charge’ in the summer between 3:00pm and 7:00pm.

So, I just measured my power usage per hour and tried to stay as low as possible because I interpreted that I would be charged (penalized) by the highest hourly usage.
But … of course, it’s not that easy.
After I inquired because I thought I was overcharged, I was told the penalty is actually based on the highest usage for rolling 60 minutes not per full hour.

So, I implemented a few new sensors, helpers, etc. to be able to record this accordingly.

This is where two automations come into play; they are both very similar and one works reliably while the other one does not.

Here’s the one that works:

alias: Record max kWh Usage per 60min
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ (states.sensor.kwh_used_last_60min.state | float(2) ) >
      (states.input_number.max_kwh_usage_per_60min.state |float(2)) }}
condition: []
action:
  - service: input_number.set_value
    data:
      value: "{{ states('sensor.kwh_used_last_60min') }}"
    target:
      entity_id: input_number.max_kwh_usage_per_60min
mode: single

And here is the one that does not:

alias: Record max kWh Usage per 60min (Peak)
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ (states.sensor.kwh_used_last_60min.state | float(2) ) >
      (states.input_number.max_kwh_usage_per_60min_peak.state |float(2)) }}
condition:
  - condition: state
    entity_id: input_boolean.electricity_peak_recorder
    state: "on"
action:
  - service: input_number.set_value
    data:
      value: "{{ states('sensor.kwh_used_last_60min') }}"
    target:
      entity_id: input_number.max_kwh_usage_per_60min_peak
mode: single

The first automation basically runs all day and gives me the highest 60-minute value; it resets at midnight and seems to be rock-solid.

The second automation is also reset to 0 at midnight but only records when the input_boolean has been switched on because we’re in a peak hour. But, more often than not, it doesn’t start to increase the _peak value when the _recorder switches to on.
I can see in the template editor that the trigger condition is true because the value of the _60min sensor is larger than the _60min_peak input_number one (which is 0 when the _recorder switches on), but the Input_number actually does not get updated until I manually run the action in the automation editor.
I can also see in the automation editor that the template trigger isn’t triggered because the blue banner doesn’t light up when it should. Once I manually ran the action in the automation editor the blue banner lights up and the value updates are happening as they should.

Any idea why the second automation is not working properly?

Or should I somehow figure out how to set it up as a sensor to begin with?

You need to add a trigger for when that switches on or off.

Also please stop using states.sensor.kwh_used_last_60min.state use states('sensor.kwh_used_last_60min')

https://www.home-assistant.io/docs/configuration/templating/#states

Agreed - very bad habit.

Re the additional trigger, I was under the assumption that it follows the logic:

  1. The input_number is set to 0 at midnight.
  2. The input_boolean switchs to on at 3pm.
  3. The last_60min value switches from e.g. 0.75 to 0.76 which is larger than 0
  4. The action is carried out and the input_number is updated

Or does it not trigger because in 3, it switches from a value that’s already above 0 to another value that’s above 0?

This:

trigger:
  - platform: template
    value_template: >-
      {{ (states.sensor.kwh_used_last_60min.state | float(2) ) >
      (states.input_number.max_kwh_usage_per_60min_peak.state |float(2)) }}
condition:
  - condition: state
    entity_id: input_boolean.electricity_peak_recorder
    state: "on"

Only triggers when the state of either sensor.kwh_used_last_60min.state or input_number.max_kwh_usage_per_60min_peak changes.

It does not trigger when input_boolean.electricity_peak_recorder changes. It only checks that after the automation is triggered. So you may miss doing your actions when this changes.

If you’re interested, the first automation can be replaced by a Trigger-based Template Sensor (no input_number required).

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: state
        entity_id: sensor.kwh_used_last_60min
    sensor:
      - name: 'Max kwh usage per 60min'
        unique_id: 'max_kwh_usage_per_60min'
        state: >
          {% set t_new = states('sensor.kwh_used_last_60min') | float(2) %}
          {{ [t_new, this.state | float(2)] | max if trigger.platform != 'time' else t_new }}

Something similar can be done to substitute your second automation. Or you can continue using the automations and input_numbers.

Thanks - I’ll give it a try.
Need to wait until Monday to see if it’s working, though - no peak times on the weekend :partying_face:

Thanks, @123
Need to wrap my brain around this, that’s what weekends are for.

This doesn’t reset the max_ sensor to 0 at midnight, it sets it to the current value of the last_60_min, correct?

Correct, because at midnight the sensor’s current value is it’s maximum value at that moment. However, if you don’t like that and prefer it be set to zero, change else t_new to else 0