Creating consistent conditions for automations

Relative newbie here. I’ve watched a zillion YouTube videos and read the docs, but I haven’t seen the solution to what I feel must be a common problem.

I have a number of rooms containing lights that I’d like to turn on using a motion sensor (all of which works fine) but in each case, I need to add a condition. Only turn on the lights if:

  • The sun is up (indicating that it’s daytime), or
  • The bedroom light is on (indicating that we’re still awake)

We don’t want the lights to go on if the cats trigger them in the night time, or if one of us wanders down the hallway at night for some reason, which would shine a bright light into the bedroom. Right now, I’m using the same condition for each room, but I’d like to centralize that.

As a retired developer, it makes me uncomfortable to duplicate such code. If the condition were to change, it would change for all four rooms that use the same condition–and I’d have to go update each automation individually.

Is there some way to “centralize” a condition like this, so I could change it once and it would apply to all the automations that use it as their condition? I feel this must be possible, but the solution is evading me. Thanks!

You might have a look at Entity Controller from HACS, which provides a very economical way of controlling lights. For example:

  - living_room_light:                               
      sensor: 
      - binary_sensor.living_room_motion_sensor_motion  
      entities: 
      - switch.living_room_socket_1
      - switch.living_room_socket_4
      delay: 600
      block_timeout: 1800
      overrides: 
      - binary_sensor.override_indoor_front

This turns two lamps in the living room on if motion is detected, and leaves them on for 10 minutes (extended if movement is detected again). If the lights are switched on manually, they will stay on for half an hour, or until they are switched off manually. If the override sensor is on, the lights will not come on.

I have several overrides for different parts of the house, which get more or less daylight during the day. All are threshold helpers derived from a single outside light sensor. You can have more than one override per light, so you could create another based on the bedroom light.

1 Like

That’s kind of what blueprints are for: recipies to repeat things.

My choice would be to create a template binary sensor, something like:

template:
  - binary_sensor:
      - name: "Sun up or awake"
        state: "{{ is_state('sun.sun', 'above_horizon') or is_state('light.bedroom', 'on') }}"

Then simply use the state of that binary sensor in your condition.

1 Like

I appreciate the other answers, and will review and study them, but this is exactly what I was seeking. I felt for sure that a template was the answer, but couldn’t figure out how to start. I will use this and research more. Thanks so much!

The HACS solution is very interesting. I’ll definitely study that!

Blueprints seem like a good answer as well, but if I need to change the condition later, would it apply to all the automations that use it? I guess so—i’ll have to try it. Thanks!