"service_data" in Event "call_service" unreliable?

I have an automation, that checks if a specific scene has triggered as condition:

automation:
  - alias: Alias name
    id: UUID
    description: 'only start if entity_id contains "scene.clean_" '
    trigger:
      platform: event
      event_type: call_service
      event_data:
        domain: scene
        service: turn_on
    condition: >
      {{ trigger.event.data.service_data.entity_id.find("scene.clean") | int>=0 }}
    action:

This runs without errors. But sometimes, when I start other scenes (not starting with “scene.clean” it throws an error:

Error evaluating condition in 'Alias name': In 'condition': In 'template' condition: UndefinedError: 'list object' has no attribute 'find'

The error led me to investigate the trigger object by listening to the event: “call_service”.
Here, the event->data ->service_data->entity_id sometimes is a string and sometimes an list object. (in code marked within ** **)

EVENT 1

{
    "event_type": "call_service",
    "data": {
        "domain": "scene",
        "service": "turn_on",
        "service_data":  { 
            **"entity_id": "scene.clean_floor"**
        } 
    },
    "origin": "LOCAL",
    "time_fired": "2021-12-21T19:50:51.835697+00:00",
    "context": {
        "id": "abcdefgh",
        "parent_id": null,
        "user_id": "0123456789"
    }
}

EVENT 2

{
    "event_type": "call_service",
    "data": {
        "domain": "scene",
        "service": "turn_on",
        "service_data": {
           ** "entity_id": [
               "scene.good_night"
           ]**
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-12-21T19:19:09.893150+00:00",
    "context": {
        "id": "abcdefghijklmnopqrstuvwxyz",
        "parent_id": null,
        "user_id": "0123456789"
    }
}

Can someone explain or give a better solution for my automation?
Thank you in advance

It might be because you can call services with entities either within a data: block or outside it. Like this:

service: light.turn_on
entity_id: light.something
service: light.turn_on
data:
  entity_id: light.something

Both will do the same thing, and I think that entity_id is the only key that you an optionally place outside the data: block. Or maybe it’s related to putting the entity_id in target: vs. data:.

In any case, you should be able to rewrite it with in, like this:

{{ "foo" in "foo" }}
{{ "foo" in ["foo"] }}

Both of those return “True”.

Thank you very much. Although I called both services the same way (via UI http://homeassistant.local/config/scene/dashboard both events (shown above) are threaded different in the output.

Anyway. Your tip with in statement brought me to the following solution, wich seems to work without any error logs:

{{ "scene.clean_" in trigger.event.data.service_data.entity_id }}  --> False @ if is object | True @ if is string
{{ "scene.clean_" in trigger.event.data.service_data.entity_id[0] }} --> True @ if is object | False @ if is string
automation:
  - alias: Alias name
    id: UUID
    description: 'only start if entity_id contains "scene.clean_" '
    trigger:
      platform: event
      event_type: call_service
      event_data:
        domain: scene
        service: turn_on
    condition:
      ## see https://community.home-assistant.io/t/service-data-in-event-call-service-unreliable/369739
      condition: or
      conditions:
        ## Conditions can be short hand see: https://www.home-assistant.io/docs/scripts/conditions/#template-condition-shorthand-notation
        - "{{ 'scene.clean_' in trigger.event.data.service_data.entity_id }}"
        - "{{ 'scene.clean_' in trigger.event.data.service_data.entity_id[0] }}"
    action:
      ...

or this:

automation:
  - alias: Alias name
    id: UUID
    description: 'only start if entity_id contains "scene.clean_" '
    trigger:
      platform: event
      event_type: call_service
      event_data:
        domain: scene
        service: turn_on
    ## see https://community.home-assistant.io/t/service-data-in-event-call-service-unreliable/369739
    condition: >
      {%- if trigger.event.data.service_data.entity_id is string -%}
        {{ 'scene.clean_' in trigger.event.data.service_data.entity_id }}
      {%- else -%}
        {{ 'scene.clean_' in trigger.event.data.service_data.entity_id[0] }}
      {%- endif -%}

The type (string or list) of the value of entity_id does appear to be related to how the scene is called.

Another user reported encountering the difference last August and I confirmed it with a simple test:

It was also confirmed for the light.turn_on service call even earlier, in April 2020:

All this to say it’s a long-standing behavior, it’s unclear why there’s a difference, and its existence does make referencing the value of entity_id a bit more complicated.

Thank you for the clarifying answer (again).
I should have done better investigation in the forums.