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:
- I have a helper toggle
- I have an automation that turns the toggle
on
when an event starts in my calendar oroff
when an event ends in my calendar - 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.
- Keep the same trigger
- Don’t set any conditions; only set the following “Then do” actions
- Create a “Define variables” that uses
calendar.get_events
to grab currently active events (using a smallduration
to mimic currently active) and assigning the variable - Create a condition template to check if
'{{ variable[''calendar.calendar_name''].events | count == 0 }}'
- 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.