Light Automation Based on Multiple Varibles

I have spent hours searching here and otherwise for advice on a specific automation but haven’t figured it out yet.

I have certain inside lights and lamps that turn on daily based on a schedule, sunset -01:00:00. This has worked well for a long time until my wife got a new job where she works nights. Problem is on the nights she works I don’t want the bedroom lights to come on until a certain time which is AFTER when the other lights will come on.

Fine, I’ll create separate automations for those lights. Another problem, she doesn’t work the same nights all the time. She gets her schedule a month at a time and enters it into our shared Google calendar which I have setup in HA.

Guess I’m looking for an automation to look at the calendar and based on her working or not, turn on the bedroom lighting at either our sunset variable or 20:15:00.

Thoughts?

There are a number of ways to address this kind of thing, but we really need more information to point you in the right direction…

How are late shifts indicated in the calendar? Do they just start later or is it an all-day event called “Late Shift”…?

Will there be days when a normal shift follows a late shift, with both shifts being represented in the same day?

In most cases what I would do would be to set up a trigger-based Template sensor of binary sensor that determines whether its a normal or late shift day. Then use that sensor as a condition in your automation. If you can clarify the details of how the calendar events are set up, we can help you construct the sensor.


If you trigger the automation both at an hour before sunset and 20:15:00, you can use set up the conditions to check that the trigger for the current run of the automation corresponds to the appropriate state of the late shift sensor:

triggers:
  - id: late
    trigger: time
    at: "20:15:00"
  - id: normal
    trigger: sun
    event: sunset
    offset: "-01:00:00"
conditions:
  - or:
      - and:
          - condition: trigger
            id: late
          - condition: state
            state: "on"
            entity_id: binary_sensor.late_shift
      - and:
          - condition: trigger
            id: normal
          - condition: state
            state: "off"
            entity_id: binary_sensor.late_shift
actions:
  - action: light.turn_on
    target:
      entity_id: light.example

She’s a 911 dispatcher and her shifts are mostly 2100 to 0700 with the rare 1700 to 0300 thrown in, usually when someone is out. The days she goes in at 2100 are the ones that I need to handle differently since she generally sleeps until about 2015 hours. (work is only 10 minutes away)

She can enter into the calendar in any format that works so I’m flexible there. New monthly schedule comes out tomorrow so I’ll begin playing with binary triggers from the calendar. Thanks!

Okay, here’s one way to construct the template sensor. Trigger off the calendar event itself, but with sufficient enough negative offset to update the sensor prior to sunset. If the start hour value of the calendar event is 21, the sensor will be set to “off”.

This method is going to assume the following:

  • The Calendar events have a consistent title. It can be whatever she wants, I’m going to use “Her Shift” as a placeholder.
  • The calendar events cover the actual hours of the scheduled shifts, i.e. they are not all-day events.
template:
  - triggers:
      - trigger: calendar
        event: start
        entity_id: calendar.YOUR_CALENDAR
        offset: "-06:00:00"
    conditions:
      - alias: Check that event is for her shift
        condition: template
        value_template: "{{ trigger.calendar_event.summary == 'Her Shift' }}" 
    binary_sensor:
      - name: Late Shift
        default_entity_id: binary_sensor.late_shift
        state: |
          {{ ((trigger.calendar_event.start | as_datetime | as_local).hour == 21 ) | default(false,true) }} 

Keep in mind this is a minimal example, there are a variety of components that could be added, or completely different constructions that could be used, to make it a bit more “bulletproof”.

Thanks! I will try to play with this over the weekend. Right now I am trying to get some Third Reality multi-function night lights to pair with a Tuya Zigbee hub at a client’s house and they are NOT playing well together! About ready to rebox them and tell client to send back to Amazon…