I was just wondering how most people handle the following situation. I have a motion detector that switches on a light when it senses motion. It turns that same switch off after not sensing motion for a period of time.
When you write your automation is it best practice to check if the light is already on and NOT attempt to turn it on (i.e., an IF statement), or do most people just keep attempting to turn that light on even if it is already on?
I ask this question because I walked into the kitchen and the switch was turned on. As I moved around in the kitchen, I noticed the indicator lights on the switch kept activating signaling that HA was attempting to turn them on even though they were already on. Just wondered what is recommended on how to handle these type of automations.
For transparency’s sake, I did go in and change all my automations that activate a switch to check before activating that switch to determine if it was already on.
I was under the impression HA knew the state and didn’t send the command when it was already in wanted state.
But I guess that is wrong then.
What integration is the light using?
Maybe this is a integration thing
I don’t think that is exactly true. It appears as if nothing happens. However if you watch the indicator on my light switch, something is actually happening. HA is sending commands to turn on the switch even though it is already on. If you look at TRACES for an automation, you can see HA is trying to send commands to turn the light on each time the motion detector is set off.
So should we as automators be placing in an IF statement to circumvent this action or should we not?
This makes me think “but should it be?” I know the mass majority of HA users do no check for device status before setting a status. But should they?
Another example where I placed in an “IF” statement was my bedtime routine. When my wife and I place our phones on the charger at night, all the lights, TV’s, Blinds, locks, alarms, etc. are set for night mode. What I found during this routine was that if the door locks were already locked (I have Yale locks), HA would still try to lock the doors causing the locks to beep in protest. I stopped this beeping by using the IF statement to check if the door was already locked and skipped sending a LOCK command if it was locked.
So as we learn and improve our automation creations, should we be pushing towards checking device status or just going back and updating when the device gripes about it? LOL…
I do not test. For the rare occasions HA is wrong about the actual state, trying it again will fix the wrong state. Testing it based on wrong assumptions will keep it in the wrong state. It is also a lot of extra code that decreases readability, needs maintaining and can have bugs. Plus I have no devices that show the ignored commands.
That is exactly why I asked the question. I would have NEVER realized that HA continuously sent the ON command if I did not have a device that showed the command. Now that I know it does, should I teach others how to avoid it at the cost of
It’s been awhile since I created automations for motion-activated lights. I just checked them and they’re all designed to turn on a light only if it’s currently off (i.e. checks the light’s state before attempting to act on it).
The additional code needed to achieve it is trivial; it minimizes communications traffic by sending only necessary commands. That’s how I prefer it.
All of the motion-activated lights have been working well for several years now.
I have found another reason to add code to check if the light is already on. For some of my lights, I have the automation ramp up the light when it turns on a bulb. One of these automations deal with the toilet area light. While in the toilet, I noticed the light seemed to flicker ever so often. What I discovered is that with the ThirdReality bulb when you have it “Ramp up” when it is given the command to turn on, it will attempt to Ramp up every time the automation sends a command to turn on. So if you have a PIR determining if you are in the bathroom that triggers your automation, every time it senses motion, the automation goes off telling the bulb to turn on. Because it “ramps up”, the bulb appears to flicker. By adding the code to NOT turn on the bulb if it is already on, the flicker does not happen.
I am not sure if this happens with other brand bulbs, but this is another good reason to add the extra code to minimize traffic to the bulb.