Automation not continually running?

Hi All,

I have an automation that checks the power level of a device, and if it is below a certain power level, then it turns the device off for 5 seconds and then turns it back on again. I am constantly finding that the device is below the threshold that I set, but it isn’t resetting - whereas at other times it does detect it and reset, but not reliably. I have also found that sometimes the automation goes off, but then when the device drops below that power level again it does not run the automation again? I have tried changing working modes etc but not luck. Is there a way to tell HA to constantly check and then reset if it is below the threshold for 5mins? Below is the code:

alias: Rig 1 > Power Consumption < Toggle
description: “”
trigger:

  • type: power
    platform: device
    device_id: 4291814630f8897e6e1468d30b3c17f5
    entity_id: a7732faf618818d50c0fa8e793be33eb
    domain: sensor
    below: 1000
    condition:
  • condition: device
    type: is_on
    device_id: 4291814630f8897e6e1468d30b3c17f5
    entity_id: cd0e6599db967922cbd4283ed893b591
    domain: switch
    action:
  • type: turn_off
    device_id: 4291814630f8897e6e1468d30b3c17f5
    entity_id: cd0e6599db967922cbd4283ed893b591
    domain: switch
  • delay:
    hours: 0
    minutes: 0
    seconds: 5
    milliseconds: 0
  • type: turn_on
    device_id: 4291814630f8897e6e1468d30b3c17f5
    entity_id: cd0e6599db967922cbd4283ed893b591
    domain: switch
    mode: restart

hard to identify code issues if it’s not formatted.

forcing some sort of polling is a real hack. have you looked at the traces and see if it got triggered and maybe eliminatedby the condition? or did it not get triggered at all?

also look at the sensor history and see what the system is seeing for the change in sensor readings. if the history logs in home assistant are indeed seeing the right values, then it’s most likely bug in your code.

also you’ll help yourself a lot if you switch from using those unreadable device_id’s and use friendly entity_id’s:

You need to post code as preformatted text (</> in the cogwheel menu in the toolbar), otherwise it’s very difficult to read.

Having said that, it looks as if your trigger will only fire when the power level of the device drops from 1000 to 999. It will not fire again until the power level rises above 1000 and then drops a second time. Is that what you intended?

Apologies I will re-format next time. Sounds like you are onto something there, yes if it drops below 1000 and stays below 1000, I’d like it to fire again.

If you’re going to turn the device off, it might be easier to create a template binary sensor that turns off when power is above 1000 and when the device is unavailable and only turns on when power is below 1000. That would give you a simple on/off trigger for the automation that would refresh each time the power level changed.

Either way, it sounds as if your device is going to be turning on and off continually and the automation is going to be running all the time. Is that a good idea?

2 Likes

when you power cycle it, if it comes back and is below 1000, you want to power cycle it again right away? that’ll destroy a lot of devices… do you want to give it some amount of time get up to where you think it should be?

Why not reformat this time so more people will look at your issue and help?

I think the only way you could do what you want is to use a “repeat-until” action.

psuedo-code

trigger the first time that the power goes below 1000 for 5 minutes

repeat:

turn off the switch, waits 5 seconds, turn on the switch, wait some amount of time for the sensor/device to stabilize

until the value is above 1000

if not then repeat the above actions

but that psuedo-code is only based on you stated desire of operation. If the value never goes above 1000 after the delay time then the automation will run forever (or until HA is restarted or the automation is reloaded).

But that may be what you want?

Thanks. I have made a automation similar to this and it seems to be working ok now.