Sanity check: how to condition action on whether or not any calendar event is active

I have an automation that will turn on a scene whenever a trigger is met. However, I do not want this to happen when I have a currently active event in my calendar.

As an example, I want to charge my car whenever it plugs in, but I do not want to charge it if I have indicated in my calendar that electricity is currently dirty or expensive. I will have a trigger when my car is plugged in, and I will have an action to charge my car. I want that action captured in an if then so that the action is only taken if my “dirty electricity” calendar has zero currently active events.

I was originally able to go about this by using two separate automations, which I saw was suggested in a different post:

  1. I have a helper toggle
  2. I have an automation that turns the toggle on when an event starts in my calendar or off when an event ends in my calendar
  3. My “charge car” automation is triggered when the car plugs in. The action only executes if the helper toggle is on.

I’m concerned this is error prone. Would the helper be stuck on if HA is offline during the event end time? I’d rather keep track of things manually than end up in a situation where my car never charges because it thinks electricity is dirty 24/7. I suppose I could create a third automation whenever HA reboots to validate the state of the toggle, but now I need to manage 3 automations for something that feels too simple to justify so much overhead.

I’m new to HA, but I felt like there must be a way to do this in a single automation. I think I have something functioning, and I’d like to have someone review my work.

  1. Keep the same trigger
  2. Don’t set any conditions; only set the following “Then do” actions
  3. Create a “Define variables” that uses calendar.get_events to grab currently active events (using a small duration to mimic currently active) and assigning the variable
  4. Create a condition template to check if '{{ variable[''calendar.calendar_name''].events | count == 0 }}'
  5. Perform action

I tested it out with a simple automation that will toggle a light switch whenever a separate light switch is toggled, but only if there are no currently (or soon-to-be) active events in my calendar.

automation.yaml
- id: 'automation_id'
  alias: test
  description: ''
  triggers:
  - type: changed_states
    device_id: device_id_first_light
    entity_id: entity_id_first_light
    domain: light
    trigger: device
  conditions: []
  actions:
  - action: calendar.get_events
    target:
      entity_id: calendar.test
    data:
      duration:
        minutes: 5
    response_variable: test_variable
  - condition: and
    conditions:
    - condition: template
      value_template: '{{ test_variable[''calendar.test''].events | count ==
        0 }}'
  - type: toggle
    device_id: device_id_second_light
    entity_id: entity_id_second_light
    domain: light
  mode: single

Does this implementation make sense? Like I said earlier, it is not even worth my time if this is error prone. Failing to charge the car is actually less problematic than what would actually happen if my automation ran incorrectly. I’d rather manage this manually than end up in a false positive or false negative situation.

Does the calendar in question only contain events that indicate that energy is dirty/expensive?

Yes, I do not need to check the summary or description for my automation. If an event is active, then electricity is dirty.

Then just use a State condition based on the state of the calendar entity.

- id: 'automation_id'
  alias: test
  description: ''
  triggers:
    - type: changed_states
      device_id: device_id_first_light
      entity_id: entity_id_first_light
      domain: light
      trigger: device
  conditions:
    - alias: "Check if calendar currently has no active events"
      condition: state
      entity_id: calendar.test
      state: "off"
  actions:
    - type: toggle
      device_id: device_id_second_light
      entity_id: entity_id_second_light
      domain: light
  mode: single

Ah, that is a lot simpler. Thank you!

Is checking calendar state mentioned anywhere in the documentation? I feel like I’m missing out on a lot of easy solutions because I don’t know about all available APIs.

The limitation of using the state of the calendar as either a trigger or condition is why I asked about the events in your calendar. The state doesn’t discriminate between all-day events or events that abut or overlap… it’s just “on” if anything is active. That often makes the state unfit for purpose because many calendars aren’t “single purpose” like yours. That’s why there are Calendar Event triggers and the calendar.get_events action.

1 Like

Of course… right at the top of the calendar docs. Thanks again.