Downsides of calling a service to turn on a light that is already on

I currently have an automation to turn on a light group of 20 individual lights every time motion is detected. If motion is detected every few seconds, I guess the service would be calling to turn on the light group even though they are already on. Are there any downsides to this? Should I be checking if the lights are already on before calling the service?

The reason it becomes tedious is because there may be an individual light that is turned off for one reason or another. I want the detected motion to turn on that individual light that is off. If I want to check for an individual light, I will need to separate the automation to check that each light is turned on, and then turn on each light individually that is not on. Is it worth the effort?

Shouldnā€™t be an issue, I wouldnā€™t worry about it. Checking the state of each entity and calling the service on only those entities that are off would probably result in more I/O anyway.

1 Like

I believe it depends on how you are turning on the 20 lights.

For example:

  • The lighting technology you are using supports groups.
  • You have created a native group containing 20 lights.
  • You use the appropriate service in Home Assistant to activate that native group.

That means you only need to transmit one command to turn on 20 lights. In that case, thereā€™s no improvement in performance or efficiency if you go through the trouble of determining which oneā€™s are on/off.

On the other hand, if you are turning on the 20 lights by sending 20 commands, one to each of the 20 lights, then thereā€™s a benefit to determine whatā€™s on/off in order to minimize traffic. All it takes is one template to determine which lights, out of the 20, are now off.

2 Likes

Okay, Iā€™ve seen similar template wizardry to iterate through a domain of lights, but this would be a subset of that group and then iterating through that subset that is ā€˜offā€™ to issue ā€˜onā€™ commands. (Iā€™m assuming something like z_wave here ?)
As youā€™ve said it, it it must be possible, but surely you will be banned from the magic circle for disclosing such wizzarding tricks ?

Go at it maestro :+1:

:rofl:

Ah yeah, thatā€™s a fair point. I wasnā€™t considering network I/O specifically

What is this ā€˜magic circleā€™ you speak of? (First rule of Fight Club ā€¦)


Here are two ways to do it. The templates in both examples produce a string containing a comma-separated list of light entities that are off.

light.garage,light.kitchen,light.porch

If none of the lights are off then the template returns none (because thatā€™s an acceptable way of indicating ā€œno entitiesā€, whereas leaving it blank is not).

In this example, the 20 lights are defined in a list:

  action:
    service: light.turn_on
    data_template:
      entity_id: >
        {% set lights = [ states.light.first, states.light.second,
                          states.light.third, states.light.fourth,
                          states.light.etc, states.light.twentieth ]
                        | selectattr('state', 'eq', 'on')
                        | map(attribute='entity_id')
                        | list | join(',') %}
        {{ lights if lights | count > 0 else 'none' }}

This version assumes you have an existing group defined (in Home Assistant) containing the 20 lights.

  action:
    service: light.turn_on
    data_template:
      entity_id: >
      {% set lights = expand('group.my_20_lights')
                      | selectattr('state', 'eq', 'off')
                      | map(attribute='entity_id')
                      | list | join(',') }}
      {{ lights if lights | count > 0 else 'none' }}

NOTE
The assumption Iā€™ve made is that the light domain contains more than 20 lights and the goal here is to just use a subset of them (i.e. just 20). The template is simplified if the goal is to use all available lights.

3 Likes

ā€œInconceivable !ā€ . . . (Princess Bride)

:rofl:

Princess Bride. Definitely a must-see during these self-isolating times.

Vizziniā€™s ā€œInconceivable!ā€ is made even more priceless by Inigoā€™s observation: ā€œYou keep using that word, I do not think it means what you think it means.ā€

Weā€™re definitely off-topic now but I canā€™t help but think thereā€™s some kind of ā€˜inside jokeā€™ when they cast Wallace Shawn to portray Vizzini, a Sicilian mob boss. Donā€™t get me wrong, Shawn is fantastic in the role but as Sicilian as ā€œmac 'n cheeseā€. Iā€™m probably over-thinking it ā€¦

1 Like

Thanks guys for your input.
My group is a light group (https://www.home-assistant.io/integrations/light.group/).
The group is comprised of lights from multiple manufacturers: xiaomi (zigbee and wifi), IKEA (zigbee), random wifi lights, and other non-smart lights that are connected to zigbee power outlets (then converted to a power switch - https://www.home-assistant.io/integrations/light.switch/).
I then call the service ā€œlight.turn_onā€ for the light group.

My light domain containā€™s about 40 lights, all separated into areas - each with their own light group. So if I were to implement code to separate them, I would apply the same to each of my groups.

With this extra information, would you say there would be a noticeable performance improvement (network, I/O, or otherwise) by implementing Tarasā€™ code? If so, I suppose I should also implement something similar to turn off only the lights that are not already off after no motion is detected for X minutes (rather than turning off the entire light group, even though some lights are already off)?

Erm !
I played with zigbee early on and had some problems (you are screwed if you turn it off at the switch, if you convice your family not to use the switch you need a fob thingy taped up next to the switch or you go (as Taras did) for voice control.
((I didnā€™t know you used NodeRed !!! Iā€™m (TBH) shocked !!! Why ? ), (and what happened to ā€˜premiseā€™ ?))
It is possible (I believe, but never take anyoneā€™s ā€˜guessā€™ as ā€˜gospelā€™ (I donā€™t even take ā€˜gospelā€™ as ā€˜gospelā€™ ! )) to group zigbee at the hub when you pair them (mainly hue i think). in which case that would be faster, else Tarasā€™ is the way to go. it would also show marginal improvements over wifi ethernet and definatly for z wave.
Suck it and see

ā€¦ especially when it comes to a land war in Asia

? Seen any ROUSā€™s ???

Iā€™m pretty used to not using the light switch now. Was a bit of a nuisance at first, but no real issues with ignoring the light switches. Iā€™m in Australia and z wave is pretty much either non-existant or super expensive. I use conbee ii as my hub, but as not all my lights are on deconz, I donā€™t use the groups function on there. I didnā€™t really see the point of grouping them on there, and then again on HA for the lights that are not on deconz. I just group all the individual lights as a HA light group.

@Burningstone uses deconz and may better advise.
Iā€™m pretty sure if you groped them on deconz and then again on HA youā€™d have the best of both worlds.
Regardless Iā€™m ā€œstronglyā€ leaning towards Tarasā€™s code as your best option.

Though itā€™s fairly easy to do without, so try it first without, test and observe, then test and time (hard to do both) - change the automation, then rinse and repeat - go with what works for you - And remeber to report back

I think Tarasā€™s post deserves the solution tick though so please do that regardless