How to use select filter on a variable with unknown format?

I am building an automation that monitors for scene changes in a specific room (based on the name of the scene). This is what I have so far (the current action is just for debugging purposes):

- id: "1634934515108"
  alias: Set Helper - Record Last Living Room Scene
  description: ""
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: scene
        service: turn_on
  condition:
    - condition: template
      value_template: "{{'living_room' in trigger.event.data.service_data.entity_id|join}}"
  action:
    - service: notify.persistent_notification
      data:
        message: "1 {{trigger.event.data.service_data.entity_id}} 2 {{trigger.event.data.service_data.entity_id|select('search','living_room')|list|join}} 3 {{'living_room' in trigger.event.data.service_data.entity_id|join}}"
  mode: single

The trigger and condition are working as intended, running whenever a “living_room” scene is activated. The next step I need to do is parse trigger.event.data.service_data.entity_id such that I can pass the scene name to an input_select helper to store it. The challange I am having is that trigger.event.data.service_data.entity_id could evaluate to any of the following 3 formats depending on how a scene is called (based on looking at the logs, not clear why this is happening, see further below):

A 'scene.living_room_warm'
B ['scene.living_room_warm']
C ['scene.living_room_warm', 'scene.bedroom_warm']

I am trying to build a filter to do this. My desired output would look the same as case A, but for any of the 3 cases. I have tried to use the following filter:

{{trigger.event.data.service_data.entity_id|select('search','living_room')|list|join}}

Output:
A
B scene.living_room_warm
C scene.living_room_warm

As you can see, this filter works for case B and C, but not for A. Can anyone provide guidance on how to proceed? Thanks.


As an aside, I do not really understand what causes case A vs B. I know that case A occurs when the entity_id in the event log is formatted like this:

event_type: call_service
data:
  domain: scene
  service: turn_on
  service_data:
    entity_id: scene.living_room_neutral

And that case B occurs when the entity_id in the event log is formatted like this:

event_type: call_service
data:
  domain: scene
  service: turn_on
  service_data:
    entity_id:
      - scene.living_room_neutral

But I don’t really understand when or how or why one vs the other occurs, or how they should be used or treated differently. Is one format legacy? Is one format default from the UI editor? Clearly the way the data can be manipulated is different. Any clarity here or a pointer to the relevent documentation would be greatly appreciated. Cheers.

The first instance is a string, not a list like the other two instances, so we can test for that and convert the string to a list.

{% set e = trigger.event.data.service_data.entity_id %}
{{ ([e] if e is string else e )|select('search','living_room')|list|join }}

Regarding the value sometimes being a string instead of a list, that’s something that’s been around forever (EDIT that’s not to say it’s widely known, just that’s it’s a long-standing quirk).

Thanks, this should be helpful to get things working. I was having troubling looking in to this “quirk”, maybe I wasn’t using the right search terms, but it seems like that kind of thing that would come up fairly often as an issue, no?

Glad to hear it!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.