Morning automation based on calendar

I have cobbled together code from all over the forums and I am pretty sure this will work, but as I have no way to test it until the morning (and if it doesn’t work, I’m in trouble), can someone look this over and confirm if it works or not?

Basically, I created a google calendar with all day events that say school on the days my kids have school and is blank if they don’t. Thus, if the calendar says school, I want my kids lights to change colors at 7am. If the calendar doesn’t say school (IE holiday or weekend), then the kids lights change colors at 8am.

For sake of security, I have switched all identifiers to generic placeholders. The sensor currently reads “True” today.

- cal_id: [email protected]
  entities:
  - device_id: school
    ignore_availability: true
    name: school
    track: true
    search: "school"
# Is Today a School Day?
      school_day:
        friendly_name: Is it a school day?
        value_template: "{{ is_state('calendar.school', 'on') }}"
alias: 'Nightlights: Mornings (School)'
description: ''
trigger:
  - platform: time
    at: '07:00:00'
    id: school
  - platform: time
    at: '08:00:00'
    id: noschool
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: school
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
          - condition: state
            entity_id: sensor.school_day
            state: 'true'
        sequence:
          - service: light.turn_on
            data:
              rgb_color:
                - 0
                - 255
                - 0
            target:
              entity_id:
                - light.son_bedroom_nightlight
                - light.daughter_bedroom_nightlight
      - conditions:
          - condition: trigger
            id: noschool
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
              - sat
              - sun
          - condition: state
            entity_id: sensor.school_day
            state: 'False'
        sequence:
          - service: light.turn_on
            data:
              rgb_color:
                - 0
                - 255
                - 0
            target:
              entity_id:
                - light.son_bedroom_nightlight
                - light.daughter_bedroom_nightlight
    default: []
mode: queued
max: 10
# Is Today a School Day?
      school_day:
        friendly_name: Is it a school day?
        value_template: "{{ is_state('calendar.school', 'on') }}"

If you use the above to create binary_sensor, I think the state can either be on or off if you check it in the Developer Tools —> States —> binary_sensor.school_day.

1 Like

I just checked, it shows “True”

Where did you check it? According to the documentation-

The sensor is on if the template evaluates as True and off otherwise. The actual appearance in the frontend (Open /Closed , Detected /Clear etc) depends on the sensor’s device_class value.

Also, you need to replace sensor.school_day with binary_sensor.school_day.

1 Like

Capture

I thought you created a binary_sensor.

1 Like

my apologies, misspoke!!! changed the OP

I think your value_template is more suitable to be used with binary_sensor instead of sensor.

That way, you can have on or off state.

1 Like
alias: 'Nightlights: Mornings'
trigger:
  - platform: time
    at:
      - '07:00:00'
      - '08:00:00'
condition: >
  {{ (now().hour == 7 and now().isoweekday() <= 5 and is_state('calendar.school', 'on')) or 
     (now().hour == 8 and is_state('calendar.school', 'off')) }}
action:
  - service: light.turn_on
    data:
      rgb_color:
        - 0
        - 255
        - 0
    target:
      entity_id:
        - light.son_bedroom_nightlight
        - light.daughter_bedroom_nightlight
1 Like

Thank you!!! Looks way more elegant. Once I confirm it works (and I have no doubt it will), I’ll mark as the Solution.

How would the template change if the time were 6:30 instead of 7?

Is that part really needed?
If the light did not turn on at 7, then it will turn on at 8.
The only collision is when the light turns on at 7 and manually turned off at 7:30.

{{ (now().hour == 6 and now().minute == 30 and....

1 Like

Thank you!!

trigger:
  - platform: time
    at:
      - '06:30:00'
      - '07:30:00'
condition:
  - condition: template
    value_template: >
      {{ (now().hour == 6 and now().minute == 30 and now().isoweekday() <= 5 and
      is_state('calendar.school', 'on')) or 
         (now().hour == 7 and now().minute == 30 and is_state('calendar.school', 'off')) }}

I assume I would need to adjust the trigger accordingly, the updated code looks like this?

1 Like

I use this in my templates for comparing the current time to a desired time:

(now().hour,now().minute) == (6,30)

Basically it is comparing tuples.

However, you don’t actually have to check if the minute equals 30. The two triggers occur at different hours so simply checking the hour is sufficient to differentiate between the two. If they both triggered at the same hour but at a different minute then you would only need to check the minute to tell them apart.

In other words, unless the two trigger times are at the same hour, you don’t have to change the template I posted.

Nothing happened this morning. Not at 7 or at 8. Sorry!

Try to see the automation’s debug trace to find out why is it not working-

Post an exact copy of the automation you are using.

alias: 'Time: Nightlights - Morning (Change to Green)'
description: ''
trigger:
  - platform: time
    at:
      - '07:00:00'
      - '08:00:00'
condition:
  - condition: template
    value_template: >
      {{ (now().hour == 7 and now().isoweekday() <= 5 and
      is_state('calendar.school', 'on')) or 
         (now().hour == 8 and is_state('calendar.school', 'off')) }}
action:
  - service: light.turn_on
    data:
      rgb_color:
        - 0
        - 255
        - 0
    target:
      entity_id:
        - light.son_bedroom_nightlight
        - light.daughter_bedroom_nightlight
mode: queued
max: 10

It doesn’t contain an id so there will be no trace available to examine. Go to Configuration > Automations and find the automation. Next to it will be a date and time representing the last time the automation was triggered. Let us know what it says.

Also, did you use the Automation Editor to create the automation or did you use a text editor to add it directly into automations.yaml? If you used a text editor, did you execute Reload Automations?


Minor point: there’s no need to use queued mode. This automation won’t ever be triggered more than once at the same time. The default single mode is sufficient.

id: '1629910316073'
alias: 'Time: Nightlights - Morning (Change to Green)'
description: ''
trigger:
  - platform: time
    at:
      - '07:00:00'
      - '08:00:00'
condition:
  - condition: template
    value_template: >
      {{ (now().hour == 7 and now().isoweekday() <= 5 and
      is_state('calendar.school', 'on')) or 
         (now().hour == 8 and is_state('calendar.school', 'off')) }}
action:
  - service: light.turn_on
    data:
      rgb_color:
        - 0
        - 255
        - 0
    target:
      entity_id:
        - light.son_bedroom_nightlight
        - light.daughter_bedroom_nightlight
mode: queued
max: 10

grabbed from trace to show ID.

I used GUI to create automation then copy and pasted via “Edit in YAML”