Help for a automation

Hi, I would like to create an automation that turns off the light based on a google calendar event.
Basically at the end of event X I have to turn off the light if another event X does not start in the half hour following the end of event X.
Example: Event X ends at 3:00, home assistant must check whether another event X starts between 3:00 and 3:30. If it starts, then it must not switch off the light, if it does not start, then it must turn off the light.
I hope you understood.
Thanks for your help.

Leonardo

There is a list_events service call for calendar.
You could trigger an automation when the event ends and use the exposed “trigger.calendar_event.end” variable for the service call start time and duration 30min + 1sec.
Use the response variable from the service call in a condition, if it has at least one event don’t turn off the light otherwise turn it off (i don’t know if that’s even possible, never tried this sort of thing xD)

If you are still stuck i will try to write an automation when i’m on my computer again

Thanks for the reply.
For now I have created this automation which however does not work as it should because if you calculate the time you will realize that since the lights for the first event are switched off after the lights for the second event have been switched on, it happens that the lights for the second event do not they start.

trigger:
  - platform: calendar
    event: start
    offset: "-0:5:0"
    entity_id: calendar.campo_cemento
condition:
  - condition: template
    value_template: "{{ 'Campo cemento illuminato' in trigger.calendar_event.summary }}"
action:
  - type: turn_on
    device_id: b2baaaf31ea5057ab6181d9eb236b8e7
    entity_id: 9bb2b49f6e07506bdcda96a7794cd46f
    domain: switch
  - wait_for_trigger:
      - platform: calendar
        event: end
        offset: "0:15:0"
        entity_id: calendar.campo_cemento
  - type: turn_off
    device_id: b2baaaf31ea5057ab6181d9eb236b8e7
    entity_id: 9bb2b49f6e07506bdcda96a7794cd46f
    domain: switch
mode: restart
trigger:
  - platform: calendar
    event: start
    offset: "-0:5:0"
    entity_id: calendar.campo_cemento
    id: "on"
  - platform: calendar
    event: end
    entity_id: calendar.campo_cemento
    id: "off"
condition:
  - condition: template
    value_template: "{{ 'Campo cemento illuminato' in trigger.calendar_event.summary }}"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - type: turn_on
            device_id: b2baaaf31ea5057ab6181d9eb236b8e7
            entity_id: 9bb2b49f6e07506bdcda96a7794cd46f
            domain: switch
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - service: calendar.list_events
            data:
              duration: "00:30:00"
              start_date_time: "{{now()}}"
            target:
              entity_id: calendar.campo_cemento
            response_variable: agenda
          - condition: template
            value_template: |-
              {{ agenda.events | selectattr('summary', 'search', 'Campo cemento illuminato') | list | count == 0 }}
          - type: turn_off
            device_id: b2baaaf31ea5057ab6181d9eb236b8e7
            entity_id: 9bb2b49f6e07506bdcda96a7794cd46f
            domain: switch

Edit: Fixed template condition to include event summary check.

1 Like

Thank You for the reply.
Unfortunately the automation don’t work. It not start. I think that the ID don’t like.
Why?

I tested the automation against a variety of cases, and it works on my instance. When testing calendar triggers it is important to understand that calendar triggers are only updated every 15 minutes or when an automation with a calendar trigger is saved, or when all automations are reloaded. So order of operations is important for testing. First set up the calendar event(s), then reload the automation either by making a change and saving it or by reloading automations in the Developer Tools > YAML tool.

Unless you are using a version older than 2021.11, trigger ID’s shouldn’t be an issue. However, on and off have special meaning, so they must be quoted as I did in my post.

I made a change and it seems to work fine.
I simply reversed the order of the lines in the first trigger section that differed from the second trigger section.

alias: Fari cemento prenotazione
description: >-
  Accende i fari cemento 5 minuti prima della prenotazione e li spegne 15 minuti
  dopo il termine della prenotazione.
trigger:
  - platform: calendar
    event: start
    entity_id: calendar.campo_cemento
    id: "on"
    offset: "-0:5:0"
  - platform: calendar
    event: end
    entity_id: calendar.campo_cemento
    id: "off"
    offset: "0:15:0"
condition:
  - condition: template
    value_template: "{{ 'Campo cemento illuminato' in trigger.calendar_event.summary }}"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - type: turn_on
            device_id: b2baaaf31ea5057ab6181d9eb236b8e7
            entity_id: 9bb2b49f6e07506bdcda96a7794cd46f
            domain: switch
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - service: calendar.list_events
            data:
              duration: "00:30:00"
              start_date_time: "{{now()}}"
            target:
              entity_id: calendar.campo_cemento
            response_variable: agenda
          - condition: template
            value_template: >-
              {{ agenda.events | selectattr('summary', 'search', 'Campo cemento
              illuminato') | list | count == 0 }}
          - type: turn_off
            device_id: b2baaaf31ea5057ab6181d9eb236b8e7
            entity_id: 9bb2b49f6e07506bdcda96a7794cd46f
            domain: switch
mode: single

Thankyou so much for the support.