Calendar Switch Y2: switch any device with a calendar [CLOSED]

Introduction

The Calendar Switch Y2 blueprint turns one or more devices on and and off according to events in a calendar. I use it for a hot water system (controlled by a zone valve connected to a smart switch), and an immersion heater with a smart switch, but the blueprint will operate any device in the domain switch.

Calendar switch Y2 can stand alone or be used as part of the suite that includes Heating X2: Schedule thermostat by calendar, and Zone Switch Z2: operate a heating zone valve from a heat demand sensor

The required helpers, timers, templates, automations etc. can be created by hand, or with the assistance of the Heating XYZ Code Generator.

Features

  • Schedule by calendar: switch one or more devices on and and off according to events in a calendar
  • Multiple devices: Two or more devices are synchronised together, i.e. turning one on turns them all on, turning one off turns them all off
  • Manual override: if a device is switched manually on its smart switch, from a dashboard or by voice assistant remains, the new setting stays in effect for a defined period (default 2 hours)
  • Away mode: turns the device(s) off when there is no one at home
  • Notifications: if devices do not respond to a new setting, go offline, or come back online
  • Logging: optional recording of key events from the switching automations into a log file
  • Robust: Graceful degradation when a device is offline
  • Battery-efficient: conserves device battery life (where applicable) by only transmitting real changes

Ancestry

Calendar Switch Y2 is essentially a stripped down version of Heating X2, which controls thermostats from calendars.

Installation

Get the Calendar Switch Y2 Blueprint


Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or use this link to copy the code manually from GitHub.


Switched devices

Calendar Switch Y2-based automations can control anything with domain ‘switch’. One or more may be listed in the same automation.

Calendars

Calendar Switch Y2 uses any Calendar to program the device(s). Typically you would create a calendar using the Home Assistant Local Calendar integration (you need Home Assistant 2022.12.1 or later). The blueprint should also work with any external calendar linked to Home Assistant that has domain calendar.

Add an Event to the calendar for each period that you want to turn the device(s) on.

  • In the Summary field put the title of the event as you want it to appear on the dashboard in the reason text. I recommend including the device name as an aid to picking up errors.

  • In the Description field add descriptive text – optional, not used by the automation.

  • Set the Event start and end times as usual. Consecutive and overlapping events are allowed, though since all events mean “on”, the only change is the name on the dashboard.

  • Set the events to Repeat daily or on specific days of the week as required.

Helpers and Timers

The blueprint requires a number of helpers and timers, which are used as global variables. Create the helpers in the UI or with YAML for each automation (device or group of devices) that you are going to create with the blueprint.

Input Text

  • Setting reason – into which the automation writes the reason for the current setting for display on dashboards. Make the length 255 characters.

Input_booleans

  • Manual state – into which the automation writes the state (on or off) specified by manual override.

  • Required state – into which the automation writes the state (on or off) currently set for the device (to optimise radio traffic and check response)

Timers

  • Manual override timer – which the automation uses for the time remaining for a manual intervention before it reverts to the calendar schedule

  • Echoblock timer – which the automation uses to time the response from the device(s) before checking its setting, and to distinguish manual changes to the set state from those made by the automation.

Logging

Logging is optional, so skip this step if you do not want it. The default blank in the blueprint logging service name indicates that no logging is required.

To enable logging

  1. If you want to keep your log fles in a folder, then add the folder to /CONFIG/ – in my example it is called log_files.
  2. In configuration.yaml add an entry for each automation like this
      notify:
        - name: immersion_heater_log 
          platform: file
          filename: log_files/immersion_heater_log.txt
          timestamp: true 
  1. Specify the service name (not the file name), exactly as defined, in the blueprint parameters (below). In my example the name is immersion_heater_log

The Automations

Create one automation for each device, or group of devices that are controlled by one calendar, using the UI. Select the Blueprint Calendar Switch Y2 and specify in the inputs the devices, calendar, helpers and timers described above, plus …

  • Logging service name – if logging is enabled, enter the logging service name exactly as specified in the configuration.yaml file. Do not include a notify. prefix. Leave blank if no logging is required.

  • Manual override period – the time period for which a manual intervention (on or off) will override the schedule. Default 2 hours.

Starter Dashboard

My recommended starter dashboard has a card for each device (or device group). Construct it as a vertical stack with the name of the device as its title, the device switch as a button sub-card, the reason text as a markdown sub-card (because the text is too long for a regular text entity) and the manual override timer in an entities sub-card (so that it can be reset).

While testing, you might like to add some more of the helper and timer entities to the dashboard cards, so that you can better see what is going on. Adding timers allows them to be cancelled from the dashboard as well.

More details

The guide “Smart Heat Your Home with Heating XYZ”, with hardware and software configuration tips and detailed instructions for using the code generator, is bundled with the Code Generator download.

Good Luck!

:+1:

Minor updates (for robustness)

  • 05-Mar-2024: 1.0.01 Added detail to the trigger line of the log entries
  • 08-Mar-2024: 1.0.02 Corrected the trigger line of the log entries when triggers have no entity_id (e.g. calendars)
  • 08-Mar-2024: 1.0.03 Tidied up logic of ACTION[6] using choose, to ensure there is always a log message
  • 08-Mar-2024: 1.0.04 Improved notifications. Additional unknown and unavailable checks every time a setting is needed. Notification automatically dismissed when device comes back online (instead of a new notification). Only ever one notification per device.
  • 13-Mar-2024: 1.0.05 Added ‘continue_on_error’ and ‘parallel’ to device service call, ‘continue_on_error’ to logs and notifications, because failure her should not stop the main device switching function

Hi Andy
I’m trying to use your Calendar_switch_y2 blueprint.
I can’t get past the runtime error: Error: Service script.logfile_entry not found
I followed your instructions in “Logging #2”, put:
`Notify:

  • name: rental_events
    platform: file
    filename: log_files/AC_triggers.txt
    timestamp: true`
    in configuration.yaml.
    what am I doing wrong?
    thanx
    r

Sorry. this is a recent change in HA that I have not yet caught up with in the instructions.

HA (for reasons best known to themselves) withdrew the facility to define log files in configuration.yaml and changed the service call to notify.send_message.

You now have to set up each log file notification service by hand using the File integration.

  1. In the integration click ADD ENTRY and choose “Notification service”.

  2. Tell it your file name … e.g. /config/log_files/AC_triggers.log. Turn timestamps off because logfile_entry adds its own.

  3. The integration sets up the Entry as File[/config/log_files/AC_triggers.txt], so you have to rename it manually to something sensible like “AC Triggers Log”.

  4. The integration sets up the entity with name “File” and entity_id notify.file, so you have to manually change it to something sensible, like
    “AC Triggers Log” and ‘notify.ac_triggers_log’

  5. Use your name (here ‘ac_triggers_log’) as the parameter for the blueprint.

  6. Don’t forget to add to your configuration.yaml file:

homeassistant:
  allowlist_external_dirs:
    - /config

Yes, this is awkward and a step backwards, but it is not of my making!

The logfile_entry.yaml script has been updated to use the notify.send_message call – make sure you have the latest version. You need to install this as a package.

Would it be possible to just have one calendar to turn on/off devices depending on the calendar event location? So depending on the event it lists next it will turn on the device according to that?

Maybe by doing an “If-then” statement so If it finds that the next event location is “Office”, than it will trigger “climate.office”

If finds next event is Kitchen will trigger “climate.kitchen”

That is an interesting idea and surely possible, but would require some new coding. The current setup uses an automation per room (location) so the setup would be several automations reading the same calendar and each matching one specified location name, which you would have to add as a new blueprint input. There is a drawback that there is no easy way of checking that a location name is valid. If you mis-spell it than nothing would happen.

That makes sense the way you did it, since you’re just going off Start & End time so it will always trigger as you said, since the way I mentioned it could cause an automation not to turn on if misspelled. The only problem is that the way I have my calendar set up is more for a business, and I have one event calendar and events setup for this year and next year, and it would be hard to separate every event.

Calendar Switch Y2 has now been consolidated with related components of my Home Assistant smart heating system into the new topic Project HEATHER.

This thread is therefore closed; please make any further comment in the new topic.