Hi all,
I’m very new to HA, riding on the learning curve… It may well be that the following is HA trivia, but I haven’t figured it out so far so here goes.
HA 2021.7.2, bundled Shelly integration.
I have a few Shelly devices in a lab, with switches / loads / whatnot and am trying to create a trivial automation on a Shelly 2.5 switch: If channel 1 on the Shelly is on for more than 5 seconds, turn it off. The thing is, I’d like for this to work even if HA (re)starts and finds the channel already “on”.
I made the simple automation pasted below. So as not to miss events, it is based on looking at the power going through the switch to the load. It mostly works: When I switch the channel to on, it shuts it off after 5 seconds. However when I (a) disable the automation (b) switch the channel to “on” (c) enable the automation, it doesn’t kick in.
I’m sure I’m missing something quite basic re how triggers and events are processed. It appears as if the automation is triggered when the Shelly sends an event (switch turned on), but not via ongoing polling.
Is the Shelly integration even supposed to poll the device, or am I being stupid? Is there a proper way to achieve this?
Because at that point, the physical device is already on and consuming more power than 0.3.
A Numeric State Trigger will trigger the moment when the value (power) increases and crosses the threshold value of 0.3. It doesn’t trigger if the value is already above the threshold.
For the numeric_state trigger, the value needs to be under 0.3 and then go passing that point for the automation to be triggerred. It won’t trigger it if the value is already above 0.3.
Your template doesn’t consider the possibility when, on startup, the power value is not greater than 0.3. If that happens the template returns nothing which represents an invalid service call.
For example, the suggested correction is based on detecting the restart of Home Assistant and not for what doron tried (enabling/disabling the automation). Do we know if that was only for testing purposes or a requirement? Doron needs to clarify the requirements.
In addition, on startup should the device be turned off after 5 seconds if power is greater than 0.3, or immediately if the device was already on for more than 5 seconds while Home Assistant was offline?
Thanks! This is really helpful, I missed that.
Is there a different way to say “if this switch is seen to be on, FOR more than time X, then do Y”?
(I resorted to looking at power consumption only due to this consideration, i.e. missing the move from off to on due to any reason).
Thank you! Great stuff.
So, this will run once upon HA startup, correct?
What about when the automation is disabled and then enabled? Or, in general, when HA just missed the event of “move from off to on” for any reason?
What I’m trying to get at is an automation that will work reliably, regardless of whether it witnessed the powering on. I’m thinking robustness. I hope I’m being reasonable - as I said I’m learning.
It won’t detect enabling/disabling the automation (is that a requirement?) and it will generate an error message under certain conditions (described in my previous post).
By using this, it will trigger on HA restart. However, on restart, it will not count until 5 seconds for the switch to be turned off - just switched off directly after HA restart.
Replace the use of a service_template with a choose that executes the service call only if power exceeds 0.3.
You should also convert the sensor’s value to float and compare it to a floating-point number, not a string. String comparisons of floating-point numbers can produce erroneous results.
BTW, service_template was deprecated in favor of simply service (many versions ago).
Would it be simpler if I’d not be looking at power, but at the switch state being “on”? As I said the reason I was looking at power to begin with was my thinking about missing the flip event. But that happened anyway So maybe I don’t need it? (the 0.3w is just made up, as in > 0 ).
What complicates it is the need to turn it off after 5 seconds of operation even at startup and enabling/disabling.
I can’t recall if there is an event produced when an automation is enabled/disabled. If there isn’t then nothing is available to trigger the automation when you re-enable the automation. I just realized it wouldn’t matter. An automation that has just been enabled cannot detect it’s own enabling.
You still need to explain how you want to handle the switch on startup. Should it be turned off immediately if it’s already on or checked if it was already on for at least 5 seconds, or simply allowed to stay on for another 5 seconds?