Automation don't trigger (event)

Made an automation yesterday that uses ‘event’ as trigger, it worked then, but today it won’t trigger…

alias: Belysning - Scene + Script
description: ''
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: scene
      service: turn_on
      service_data:
        entity_id: scene.softlight
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.mi_outside_illuminance
            above: '400'
        sequence:
          - service: script.belysning_dag
    default:
      - service: script.belysning_dawn
mode: single

If I listen to the event ‘call_service’ and activate the scene i get the following:

Event 0 fired 12:52:
{
    "event_type": "call_service",
    "data": {
        "domain": "scene",
        "service": "turn_on",
        "service_data": {
            "entity_id": [
                "scene.softlight"
            ]
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-12-04T11:52:29.012564+00:00",
    "context": {
        "id": "xxx,
        "parent_id": null,
        "user_id": "xxxx"
    }
}

Anyone that has a clue or should I file a bug report? I’m on core-2021.11.5, supervisor-2021.10.8
and Home Assistant OS 6.6

that won’t work because entity_id is a list. You’ll have to check for the entity_id in the list in a template condition.

alias: Belysning - Scene + Script
description: ''
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: scene
      service: turn_on
condition:
- condition: template
  value_template: >
    {{ 'scene.softlight' in trigger.event.data.service_data.entity_id }}
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.mi_outside_illuminance
            above: '400'
        sequence:
          - service: script.belysning_dag
    default:
      - service: script.belysning_dawn
mode: single
1 Like

It works!
If I want to match on more than one scene in the condition template, how would that look?

- condition: template
  value_template: >
    {{ trigger.event.data.service_data.entity_id | join() in ('scene.softlight', 'scene.2', 'scene.3')  }}

EDIT: If your service call only turns on one scene the above edited template will work, but Petro’s template below is more robust because it allows for the option of having multiple scene entity’s activated in one service call.

1 Like

It’s a bit tricky, what @Didgeridrew posted will not work, it will always fail because you can’t check a list in a tuple unless the tuple contains an exact copy of the list. This however should always work:

{% set my_scenes = 'scene.softlight', 'scene.2', 'scene.3' %}
{{ trigger.event.data.service_data.entity_id | select('in', my_scenes) | list | length > 0 }}
2 Likes

In case others run into this too and need an additional example.
Also, I think this could need some improvement in the visual editor for the automations.

I had tried all these triggers for an event, which all of the bellow did NOT work:

triggers:
  - platform: event
    event_type: my_button
  - event_type: my_button
    event_data:
      event_type: multi_press_1
    trigger: event
  - event_type: my_button
    event_data:
      multi_press_1: true
    trigger: event
  - event_type: event.my_button
    event_data:
      event_type: multi_press_1
    trigger: event
  - trigger: state
    entity_id:
      - event.my_button
    attribute: event_type
    to: multi_press_1

Then this worked:

trigger:
  - platform: state
    entity_id: event.my_button
condition:
  - condition: template
    value_template: >
      {{ 'multi_press_1' in trigger.to_state.attributes.event_type }}

My event looked like this in developer tools, event, listen:

event_type: state_changed
data:
  entity_id: event.my_button
  old_state:
    entity_id: event.my_button
    state: "2024-12-04T08:40:19.692+00:00"
    attributes:
      event_types:
        - multi_press_1
        - multi_press_2
        - long_press
        - long_release
      event_type: multi_press_1
      previousPosition: 1
      totalNumberOfPressesCounted: 1
      device_class: button
      friendly_name: My Button
    last_changed: "2024-12-04T08:40:19.693077+00:00"
    last_reported: "2024-12-04T08:40:19.693077+00:00"
    last_updated: "2024-12-04T08:40:19.693077+00:00"
    context:
      id: 01JE8D7JZDTV6CV2XW2VY9RWAX
      parent_id: null
      user_id: null
  new_state:
    entity_id: event.my_button
    state: "2024-12-04T08:40:20.696+00:00"
    attributes:
      event_types:
        - multi_press_1
        - multi_press_2
        - long_press
        - long_release
      event_type: multi_press_1
      previousPosition: 1
      totalNumberOfPressesCounted: 1
      device_class: button
      friendly_name: My Button
    last_changed: "2024-12-04T08:40:20.697183+00:00"
    last_reported: "2024-12-04T08:40:20.697183+00:00"
    last_updated: "2024-12-04T08:40:20.697183+00:00"
    context:
      id: 01JE8D7KYSTHASH03SX6M1QXF6
      parent_id: null
      user_id: null
origin: LOCAL
time_fired: "2024-12-04T08:40:20.697183+00:00"
context:
  id: 01JE8D7KYSTHASH03SX6M1QXF6
  parent_id: null
  user_id: null

feeding the above into GPT will make it output the correct code if you give it my example.

And here another full working example with such an event as above, as GPT generated it after receiving the above example, the full json of the event and given the instruction

single press should toggle light.veranda
double press should toggle light.aussenlicht_sud
long press should toggle light.kueche_oberlicht

Please write the full automation

alias: Handle Veranda Taster Button Events
description: Automation for single, double, and long press actions on Veranda Taster
trigger:
  - platform: state
    entity_id: event.veranda_taster_button
condition: []
action:
  - choose:
      # Single Press - Toggle Veranda Light
      - conditions:
          - condition: template
            value_template: >
              {{ 'multi_press_1' in trigger.to_state.attributes.event_type }}
        sequence:
          - service: light.toggle
            target:
              entity_id: light.veranda
      # Double Press - Toggle Außenlicht Süd
      - conditions:
          - condition: template
            value_template: >
              {{ 'multi_press_2' in trigger.to_state.attributes.event_type }}
        sequence:
          - service: light.toggle
            target:
              entity_id: light.aussenlicht_sud
      # Long Press - Toggle Küche Oberlicht
      - conditions:
          - condition: template
            value_template: >
              {{ 'long_press' in trigger.to_state.attributes.event_type }}
        sequence:
          - service: light.toggle
            target:
              entity_id: light.kueche_oberlicht
    default: []
mode: single

Bit shorter with some templating in the action:

triggers:
  - trigger: state
    entity_id: event.veranda_taster_button
conditions:
  - "{{ trigger.to_state.attributes.event_type in ('multi_press_1','multi_press_2','long_press') }}
actions:
  - action: light.toggle
    target:
      entity_id: >
        {{ { 'multi_press_1': 'light.veranda',
             'multi_press_2': 'light.aussenlicht_sud',
             'long_press': 'light.kueche_oberlicht' }
             .get(trigger.to_state.attributes.event_type) }}
1 Like

cooooooooooooooool! :slight_smile: