Automating Power Sensor within Time Frame

I want to neaten this thread up, so I’ve changed all the information to reflect an accurate example. That is me turning on the kettle which boils for about 45 seconds at a draw of 1950W-2100W. The only thing I haven’t changed is Fridge -> Kettle in the code.

@Jer78
So with everything you’ve suggested the code looks as follows and the only time the code triggers currently is when I start Home Assistant.

The scenario is as follows:
I run HA with the Kettle off so of course the automation will trigger as the draw is below 1900W.
I then turn the Kettle on and see the Draw sit between a value of around 1900 and 2100, at this point after 10 seconds the automation should trigger but it does not.

  - platform: template
    sensors:
      fridge_drawing:
        value_template: '{{ states.sensor.Fridge_Draw.state | int }}'
  - platform: mqtt    
    name: "Fridge Draw"
    state_topic: "tele/sonoff2/ENERGY"
    qos: 0
    unit_of_measurement: "W"
    value_template: "{{ value_json.Power | int }}"

Automation

  - alias: If Fridge Power Between 1900W-2100W Restart
    hide_entity: False
    initial_state: on
    trigger:
      - platform: numeric_state
        entity_id: sensor.fridge_drawing
        above: 1900
        below: 2200
        for:
          seconds: 10
      - platform: homeassistant
        event: start
    condition:
      - condition: template
        value_template: "{{ states.sensor.Fridge_Draw.state | int < 1900 }}"     
    action:
      - service: switch.turn_off
        entity_id: switch.Fridge
      - delay: 00:00:10
      - service: switch.turn_on
        entity_id: switch.Fridge

@HellfireZA Hmm…really not sure why it’s not triggering. Try removing “above: 1900” and just see if it triggers when it goes below 2200. That way there’s less parameters for it to meet and easier to troubleshoot.

@HellfireZA
Even though I’m still new to HA, I’ll give this a shot. :thinking:

I see that you are using a numeric_state trigger. In the documentation on automation triggers, it shows that the for is supported.

However, according to the current documentation on automation templating, the numeric_state platform does not support trigger.for in the platform’s available data.

Is it possible that using for only works for state triggers, and not numeric_state triggers?

Hi guys.

So although this works:

  - alias: If Test Power is less than 1900W Restart
    hide_entity: False
    trigger:
        platform: template
        value_template: "{{ states.sensor.Test_Draw.state | int < 1900 }}"     
    action:
      - service: switch.turn_off
        entity_id: switch.Test
      - delay: 00:00:10
      - service: switch.turn_on
        entity_id: switch.Test

That works, if the value drops to 1700W, the automation will kick in and the “Test” will turn off, wait 10 seconds and turn back on. The problem is though that if it turns back on and it’s still under 1900W, the automation won’t trigger again, so how do I resolve that issue? Thanks

Anyone able to help me with this please?

You could write a second automation that gets triggered by the switch being turned on (maybe with a small time delay?) and a condition that checks the power draw, and if it’s below 1900W kicks off the restart again.

Try this value template instead.

value_template: '{% if states.sensor.Test_Draw.state | int < 1900 %}true{%endif%}'

Hey @Jer78 thanks for lending a hand again.

I did try that but just like the previous automation it only occurs once.

@HellfireZA the only other way is to have a time based trigger (example run every 5 mins) and use the template in a condition. I’d write it out for you but I’m on my phone right now, but if you need an example, I can post later tonight

@Jer78 That would definitely work yes, if you could write out an example for me please I’d very much appreciate it thanks.

  - alias: 'Test Power is less than 1900W Restart'
    initial_state: 'on'
    hide_entity: 'false'
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    condition:
      - condition: template
        value_template: '{% if states.sensor.Test_Draw.state | int < 1900 %}true{%endif%}'   
    action:
      - service: switch.turn_off
        entity_id: switch.test
      - delay: 00:00:10
      - service: switch.turn_on
        entity_id: switch.test
1 Like

Thank you so man, it worked like a charm :slight_smile:

1 Like