Only trigger if power consumption increases between certain range

I have an infrared sun shower, which consumes somewhere between 1445 watts and 1460 watts of power. Since the sun shower isn’t smart, and I don’t want to open the wiring I’m looking for a way to only trigger the automation if the total power consumption of our energy meter increases between 1445 and 1460 watts. It should trigger the lights in the shower cell and should turn the lights off if the total power consumption drops again with the range of 1445 and 1460 watts.

I have tried creating a template, but I’m a bit stuck at the moment…

If the value change is immediate you could use a trigger-based template sensor, something like:

template:
  - trigger:
      - platform: state
        entity_id: sensor.total_power
        not_to:
          - unknown
          - unavailable 
        not_from:
          - unknown
          - unavailable
    binary_sensor:
      - name: Sun Shower Power 
        state: |
          {% set diff = (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | abs %}
          {{ 'on' if 1445 <= diff <= 1460 else 'off' }}

But if the total change actually takes a few state changes to happen, you will need to use a statistics-based sensor like Trend or Derivative integrations.

Maybe you could also use an ESP with an infrared sensor, it would probably be way more reliable and faster to respond.

This is the peak in power consumption I see happening:

The power usage is very consistent, it does however has 2 lamps, that’s why you see the short jump to full power.

I’m a bit confused now, since the latest updates the whole automation panel changed. The current yaml code looks like this now:

alias: Badkamer - Sunshower v2
description: ""
trigger:
  - platform: template
    value_template: |-
      - platform: state
          entity_id: sensor.p1_meter_3c39e72b22dc_active_power
          not_to:
            - unknown
            - unavailable 
          not_from:
            - unknown
              - unavailable
          binary_sensor:
            - name: Sun Shower Power 
              state: |
                {% set diff = (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | abs %}
                {{ 'on' if 1445 <= diff <= 1460 else 'off' }}
condition: []
action:
  - service: notify.telegram_message_channel
    metadata: {}
    data:
      message: Sunshower aan
      title: Badkamer - Alert
mode: single

Is this correct?

That’s going to kill the reliability of the simple trigger on the Template sensor. You could still try it, but you would need one or more additional triggers. For example you could set up an automation with a similar trigger as that post above, but with a value range for the first step, then a Wait for trigger action for the change from the first value to the second. If that passes have an action to raise an event to trigger the template sensor…

But I think you will probably be better off using one of the statistical sensors.

In any case, Edwin’s suggestion to consider adding alternative sources of data is a good one.

Any time you don’t have a direct source of truth, it’s worth considering setting up a Bayesian binary sensor to incorporate multiple indirect sources using probability to get a reliable trigger.

No, what I posted is a trigger-based template binary sensor not an automation. It must be set up in your configuration.yaml file (or any properly merged file for the template integration). The binary sensor then becomes the trigger for your “light turn on/off” automation.

I you want to test the trigger in an automation it would be more like:

trigger:
  - platform: state
    entity_id: sensor.p1_meter_3c39e72b22dc_active_power
    not_to:
      - unknown
      - unavailable 
    not_from:
      - unknown
      - unavailable
condition: 
  - condition: template
    value_template: |
      {% set diff = (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | abs %}
      {{ 1445 <= diff <= 1460 }}
action:
  - service: notify.telegram_message_channel
    metadata: {}
    data:
      message: Sunshower aan
      title: Badkamer - Alert
mode: single