Automating Power Sensor within Time Frame

Ok no more errors, so progress :slight_smile:

The automation doesn’t work though, even if I take the
for:
seconds: 30

So I guess that’s where the problem lies now.

Can you paste your latest automation for me to look at? Also, can you verify the new sensor we made is working correctly?

If I go to states under Developer tools this shows:
Untitled

So I presume that means it’s working

  - alias: If Rig Power Bellow 200W Turn Off
    hide_entity: False
    trigger:
      platform: numeric_state
      entity_id: sensor.fridge_volt
      below: 300
#      for:
#        seconds: 30
    action:
      - service: switch.turn_off
        entity_id: switch.Fridge
      - delay: 00:00:10
      - service: switch.turn_on
        entity_id: switch.Fridge

Let’s make a few modifications then. I think the problem could be a combination of things so let’s add another trigger. If you restart HA while the watt is under 300 it probably won’t trigger because it’s looking for it to be above to begin with and then go below. So let’s fix that with another trigger and a condition. Then we will turn this automation on by default. Finally, the delay may need quotes. So try this:

  - alias: If Rig Power Bellow 200W Turn Off
    hide_entity: False
    initial_state: on
    trigger:
      - platform: numeric_state
        entity_id: sensor.fridge_volt
        below: 300
        for:
          seconds: 30
      - platform: homeassistant
        event: start
    condition:
      - condition: template
        value_template: "{{ states.sensor.fridge_voltage.state | int < 300 }}"
    action:
      - service: switch.turn_off
        entity_id: switch.fridge
      - delay: "00:00:10"
      - service: switch.turn_on
        entity_id: switch.fridge
1 Like

Not really related to your original question, but you are only monitoring voltage correct? Your voltage won’t/shouldn’t ever change unless you had a power outage, so I don’t see how this automation would ever trigger. It appears you want to be monitoring power, which would be measured in watts (not volts).

Maybe I’m not understanding what you are trying to do?

@brett5150: I think you made a good observation here.

From a different thread I found the following of what details a Sonoff can provide:

Message   | Unit | Description
----------|------|-----------------------------------------------------
Yesterday | kWh  | Total Energy usage between 00:00 and 24:00 yesterday
Today     | kWh  | Total Energy usage today from 00:00 until now
Period    | Wh   | Energy usage between previous message and now
Power     | W    | Current power load
Factor    |      | The ratio of the real power flowing to the load to
          |      |   the apparent power in the circuit 
Voltage   | V    | Current line voltage
Current   | A    | Current line current 

@HellfireZA: You probably want to interrogate the Power value instead of Voltage to see if your appliance is running or not. Otherwise you may constantly restart your fridge because the voltage will never go above 230V :slight_smile:

Sorry for the confusion on the naming guys, initially I was going to test with Watt draw but the test POW I’m using has nothing connected to it at all so the W is always 0. The Voltage does change slightly between 233-240, so decided to use that since once I’ve got that right I can implement with correct values. So ignore the name, and know that I’m working with voltage here. As mentioned at the top, I did get it working but without the time lapse.

@Jer78

All code added and no errors:
Couple of things, the delay definitely works without the " because when I manually trigger the automation the delay occurs.

I’ve just tried it now, when I restart HA it triggers the automation immediately.
I guess that is because of this section:

  • platform: homeassistant
    event: start
    condition:
  • condition: template
    value_template: “{{ states.sensor.fridge_voltage.state | int < 300 }}”

Yes, that’s correct. The trigger on homeassistant start and a condition that it’s < 300 was needed for testing since if the sensor is reporting 229 it would have to go above 300 first then below to trigger if we didn’t have it in there. You may just want to leave it, its up to you. But I wanted to make sure the automation was indeed working.

1 Like

@Jer78 do you have any other suggestions I could try to get the timing to work please. As I said in my top post, I can get the automaton to at least work like that,

I’m not sure what you mean by timing. Does the for statement not work?

As in the part of the automation were “for” is included doesn’t work. The only time the automation triggers is when I restart home assistant, it doesn’t activate again after that, even when the value changes.

Did you verify it went above 300 then below for at least the amount of time specified?

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.