Switch Automation

Good afternoon,

I am trying to set an automation where every few hours, HA checks to see if any of my smart switches are set to “off” and to turn them “on”. When it turns them “on”, I’d like it to automatically turn the individual smart lights powered by that switch off.

Is there a way to set this up as a rule for all (or most) switches in my house? I don’t want to immediately turn the switch back “on” in case there is a reason (other than ignorance) that the switch was used to override the induvial lights.

Why do you have smart switched powering smart bulbs?

1 Like

I prefer the smart bulbs so I can change the light color, control dim levels, turn on 2/5 lights on a 5 bulb connection, etc.

The problem is if I have a light switch, the kids or others turn off the switch which breaks the automation.

I’ve tried just bypassing the switch and connecting the wires to create an always on connection, but the downside to that is that I can not turn on or off the lights if I have no internet (or messed up my HA configuration.)

The smart switch fixes this problem as it gives me a manual way to turn off (or on) the lights while still enabling the benefits of the induvial lights.

The switches I have now are wifi fi (tasmota) but everything else is zigbee.

Using ESPHome to make the switch smart?

Tasmota using sonoff switches.

Hi, @JackieBrown - Here’s what I would do:

- alias: Auto-Check-Switches
  - platform: time
      - "12:00:00"
      - "18:30:00"
  - platform: time_pattern
    hours: /5
  - service: switch.turn_on
        - switch.light_switch_one
        - switch.light_switch_two

- alias: Turn off lights when smart switch turns on
    - platform: state
        - switch.light_switch_one
        - switch.light_switch_two
      to: 'on'
      from: 'off'
  - service: light.turn_off
      entity_id: >
        {% if   "light_switch_one" in trigger.entity_id %} light.switch_one_light_one, light.switch_one_light_two
        {% elif "light_switch_two" in trigger.entity_id %} light.switch_two_light_one, light.switch_two_light_two
        {% endif %}

It’s two automation. The first automation checks the switches, and the second automation turns the lights off if a switch changes from off to on.

First automation: I provided two options for triggers - The first trigger is for specific times of day. In this case, I selected noon and 6:30 pm. The second option is to trigger every 5 hours using the time_pattern platform. You can use one or both, depending on what you’re after.

Second automation: This one triggers only when one of the switches goes from off to on. The template checks to see which switch triggered, and then turns off the lights attached to that switch.

There are other timepattern` options that may be better suited to what you want to do. Those are described here: https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger.

Let me know if this doesn’t work for you!

If the smart switches are integrated with Home Assistant then it knows precisely when the smart switch’s state changes from on to off and you don’t need to periodically check their state.

Create an automation with a State Trigger when the switch changes state from on to off. The automation’s action can turn them back on and also turn off the associated smart lights.

- alias: 'example 888'
  - platform: state
    entity_id: switch.smart_1, switch.smart_2
    to: 'off'
  - service: switch.turn_on
    entity_id: switch.smart_1, switch.smart_2
  - delay: '00:00:05'
  - service: light.turn_off
    entity_id: light.smart_one, light.smart_two

I like this option, but I don’t think it’s what he wanted - he wants to let the switches stay off for a while:

Maybe move the delay to the top of the actions? Or add a for to the trigger - there we go. Will that work? Something like this (pick hours or minutes, probably not both):

  - platform: state
    entity_id: switch.smart_1, switch.smart_2
    to: 'off'
      hours: 1
      minutes: 10

Then remove the delay from the action section.

As a slight aside, I read somewhere (I can’t find it now) where the delay can cause some bottlenecking problems by stalling the automation. Is that true? After reading it, I try to not use delay but I’m now thinking if you’re suggesting, I’m going down the wrong path. I always set timers and then have an automation with a python script that handles all my timers when they finish. Thoughts?

My thoughts are that there’s no need to periodically check the switches as originally proposed in the first post.

What I suggested was to simply use the switch’s state-change (from on to off). Without a doubt the example I posted can be tweaked to meet precise requirements but the primary goal was to show it isn’t necessary to employ periodic checks

As for this statement:

It hasn’t been explained what kind of mind-reading technique will be used to determine the user’s intentions.

I should have been more specific - I was referring to using a delay that could hold up an automation. But now, with mode being available, I don’t know if that’s even a concern any more.

Seems pretty clear with my limited mind-reading capabilities (my wife says I have none) :upside_down_face:

Seems clear when you chop the sentence down to just what you want to see. :thinking:

What’s unclear is when you read the OP’s entire sentence. Turning off the switch is undesirable except when the user desires it. Wonderful, so what clues are available, to an automation, to determine the difference?

Having an automation periodically check the switch’s state is ‘minimum viable product’ and leaves a lot to be desired. It allows the switch to remain off for, worst case, the length of the polling period. Worst of all, if the user really want the switch to be off, too bad because it gets turned off at the next polling cycle.

Do the switches even need to actually switch anything. Just bypass them so the lights are live all the time.
The switch state change could be used to turn the lights on and off as needed.

I apologize as I thought I had explained the reason why I would want to do this.

I basically always want the switch on expect for the case of a network issue or as a bypass in case I break something in HA.

Issues I have had in the past:

  • When there is no switch (wire to always send power): Alexa is offline, I broke something in HA, or some other reason I have lost control of the indivual bulbs.
  • My lights are no longer able to change state without grabbing a latter and unscrewing the light then screwing it back in (or flipping the main breaker)
  • When I used a smart switch with no physical override
    • If a problem happened with the switch, or any of the problems I listed above, I am back to grabbing a latter to unscrew light bulbs
  • When I used a dumb switch
  • Kids are people unfamilar with the automation would turn off the switch and break all my light automations
  • Using a smart switch with physical buttons and smart bulbs
  • Best of all worlds. If something is wrong with my newtork or HA, is still can turn on or off the lights.
  • If someone turns off the lights by the switch without understanding the setup up, the issue can be fix through automation
  • I also now have control of the brightness of the bulbs and if I even want them all on by default (or based on the time of day)

In the past, I used smart switches (with no physical button)

I didn’t question why you have smart bulbs controlled by smart switches.

My question is how can an automation know if a switch that was turned off was intentional or accidental?

Was the switch turned on by you on purpose or by one of your children by mistake? Being able to differentiate between the two is important.

Without being able to tell the difference, the automation will turn it back on even if you wanted it off. It’s irrelevant when it turns it back on (immediately, in 5 minutes, or according to a schedule) the point is it will be turned on regardless of the reason it was turned off. That qualifies as a ‘dumb’ automation when it blindly reverts your actions.

This looks like what I need. I’ll try it out as soon as I get home.

If you don’t care if the automation turns the switch back on, even if you intentionally wanted it to be off, you can use the following simple automation. It allows the switch to remain off for no more than 30 minutes and then sets it to on.

- alias: 'Example 1111'
  - platform: state
    entity_id: switch.first, switch.second
    to: 'off'
    for: '00:30:00'
  - service: switch.turn_on
      entity_id: "{{ trigger.entity_id }}"

Replace the switch names with your own and adjust the time period from 30 minutes to whatever you prefer.

1 Like

I’ll try this as well.

I really can’t think of a situation I would want the switch off if HA is working. The reason I don’t want it instant is

  • I can see people pressing the button over and over before the automation turns the individual lights off
  • I don’t want to annoy my fiancée if she manually does something and it’s immediately undone.
1 Like

One possibility is to make the automation more clever by having it respond correctly to the user’s intentions.

They turn off the switch because they want to turn off the light. However, they don’t realize that the switch should remain on all the time and the light should be controlled separately.

The automation can detect when the switch is turned off, it turns the switch back on immediately but also turn off the light. That’s effectively the result the user wants: when they turn off the physical switch the light should turn off.

If Home Assistant is dead, the automation is also dead, and the switch’s operation remains unchanged. on startup, you can have an automation that turns on any switch that is currently off (and set the associated light to whatever state you prefer).

1 Like

And that, my good friend, is the struggle I (maybe all of us who don’t live alone?) have - in my case it’s my wife of over 40 years. I learned after several (sometimes passionate) debates that in our home, I needed to preserve what looks like the original dumb switches. She almost threw out the motion sensors when the lights turned off on her during the middle of a workout (yes, a bug in my motion sensor logic). And it took a while to get to the point where she stopped turning off the lamps (with smart bulbs) with the little rotator switch on the lamp…

But it’s all fun! :wink:

1 Like

I know this one too well, having been ‘hoisted by my own petard’ several times (bathroom light goes out while showering leaving me in the dark … and no amount of waving through the shower door convinces the light to turn back on). It took several revisions to get the logic sufficiently smart and convenient.

I believe the key to making this error-prone process more acceptable is to explain, to one’s family members, what you are attempting to achieve. Recruit them as ‘acceptance testers’ as opposed to unwitting victims of your back-firing experiments.

Imagine a family member who likes tinkering with the family car, without explaining the changes. Every time you get behind the wheel, what happens next is a crap-shoot (with ‘car fails to start’ high on the list of outcomes). Who wouldn’t become frustrated with their ‘hobby’?

Fortunately, my spouse is a willing, skilled, and patient ‘acceptance tester’ who understands the goal and provides feedback and suggestions when things don’t go to plan.

1 Like