Automating Power Sensor within Time Frame

So after reading quite a few posts I was able to get the switch for the Sonoff POW to turn off when a value below a certain number was recorded. My problem is I can’t add a “for” attribute into the automation because the platform is of type “template”.

I want the reading to be below a certain value for a specified time, and then the device must turn off and on. Here is what I have and it works, but not the “for a certain time part”

  - alias: If Fridge Power Bellow 300W Turn Off and On
    hide_entity: False
    trigger:
      platform: 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

You could create a template sensor first, and then use the state of that sensor in your trigger.

I’m not a coder, could you please advise me.
My current sensor looks as follows:

  - platform: mqtt    
    name: "Fridge Voltage"
    state_topic: "tele/sonoff2/ENERGY"
    qos: 0
    unit_of_measurement: "V"
    value_template: "{{ value_json.Voltage | int }}"

If I’m understanding this correctly, you want to watch if the fridge goes below 300W for a specified period of time, then quickly turn off and back on.

Try adding this sensor in addition to the one you have:

sensor:
  platform: template
  fridge_volt:
    value_template: '{{ states.sensor.fridge_voltage.state | int }}'

and then your automation would be this:

  - alias: If Fridge Power Bellow 300W Turn Off and On
    hide_entity: False
    trigger:
      platform: state
      entity_id: sensor.fridge_volt
      below: 300
      for: 
        minutes: 10
        seconds: 0
    action:
      - service: switch.turn_off
        entity_id: switch.fridge
      - delay: '00:00:10'
      - service: switch.turn_on
        entity_id: switch.fridge

Ok I’ve done the following:

sensor:
  platform: template
  fridge_volt:
    value_template: '{{ states.sensor.Fridge_Voltage.state | int }}'
    
sensor:    
  - platform: mqtt    
    name: "Fridge Voltage"
    state_topic: "tele/sonoff2/ENERGY"
    qos: 0
    unit_of_measurement: "V"
    value_template: "{{ value_json.Voltage | int }}"

Then I redid my automation to:

  - 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

I had to change state to numeric_state otherwise it threw an error. The automation isn’t triggering though.

You’re right, it was supposed to be numeric_state.

For the sensors, you shouldn’t duplicate “sensor”. Again, my bad for putting it in there: Try this:

sensor:
  - platform: template
    fridge_volt:
      value_template: '{{ states.sensor.Fridge_Voltage.state | int }}'    
  - platform: mqtt    
    name: "Fridge Voltage"
    state_topic: "tele/sonoff2/ENERGY"
    qos: 0
    unit_of_measurement: "V"
    value_template: "{{ value_json.Voltage | int }}"

That throws an error:

The following components and platforms could not be set up:

sensor.template

I’m really off today. Sorry about this. Should really double check the documentation.

Let’s try this again:

sensor:
  - platform: template
    sensors: 
      fridge_volt:
        value_template: '{{ states.sensor.Fridge_Voltage.state | int }}'    
  - platform: mqtt    
    name: "Fridge Voltage"
    state_topic: "tele/sonoff2/ENERGY"
    qos: 0
    unit_of_measurement: "V"
    value_template: "{{ value_json.Voltage | int }}"

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?