Error rendering data template: UndefinedError: 'str object' has no attribute 'object_id'

so after updating to 118, I now see this for the first time using an automation that has been there for ever…

some how the var entityid which gets passed to the script isn’t correct, and I don’t see why, because several other scripts/automations I use don’t return this error. Please have a look with me?

automation:

  - alias: Trigger alarm while armed
    trigger:
      platform: state
      entity_id:
        - binary_sensor.attic_sensor_motion
        - binary_sensor.auditorium_sensor_motion
        - binary_sensor.front_room_sensor_motion
...
        - binary_sensor.master_bedroom_sensor_motion
      to: 'on'
    condition:
      - >
          {{trigger.to_state is not none and
            trigger.from_state is not none and
            trigger.to_state.state != trigger.from_state.state}}
      - >
          {{states('alarm_control_panel.ha_rpi4_alarm') in ['armed_away','armed_home']}}
    action:
      - service: alarm_control_panel.alarm_trigger
        entity_id: alarm_control_panel.ha_rpi4_alarm

      - condition: state
        entity_id: input_boolean.notify_alarm
        state: 'on'
      - service: script.alarm_triggered
        data:
          entityid: >
            {{trigger.entity_id}}

and script:

  alarm_triggered:
    alias: Alarm triggered
    mode: queued
    sequence:
      service: notify.mobile_app_calltheboss
      data:
        title: >
          {% set room = entityid.object_id.split('_sensor_motion')[0].replace('_',' ')|capitalize %}
          Alarm triggered in the {{room}}
        message: >
          {% set name = entityid.object_id.replace('_',' ')|capitalize %}
          {{as_timestamp(now())|timestamp_custom('%X')}}: {{name}} triggered the alarm, check whats up!

while this:

      - service: >
          script.{% if is_state('input_boolean.notify_announcement','on') %}announce_presence
                 {% elif is_state('input_boolean.notify_presence','on') %}notify_presence
                 {% else %}continue
                 {% endif %}
        data:
          trigger: Presence tracking
          entityid: '{{trigger.entity_id}}'
          tostate: '{{trigger.to_state.state}}'
          fromstate: '{{trigger.from_state.state}}'

and script:

  notify_presence:
    alias: Notify presence
    mode: queued
    sequence:
      service: notify.notify
      data:
        title: >
          {{trigger}}: {{state_attr(entityid,'friendly_name')}} notification
        message: >
          {% set name = state_attr(entityid,'friendly_name') %}
          {{as_timestamp(now())|timestamp_custom('%X')}} :
          {% if tostate == 'not_home' %}
            {{-name}} left {{fromstate}}
          {% elif fromstate == 'not_home' %}
            {{-name}} arrived at {{tostate}}
          {% else %}
            {{-name}} left {{fromstate}} and arrived at {{tostate}}
          {% endif %}

works just as it has always done

I can manipulate the string like this of course:

{% set entityid = 'binary_sensor.attic_sensor_motion' %}
{% set room = entityid.split('.')[1].split('_sensor_motion')[0].replace('_',' ')|capitalize %}
{{room}}

but rather like to manipulate the object itself:

this works just fine, all in the 1 automation:

      - service: notify.mobile_app_calltheboss
        data:
          title: >
            {% set room = trigger.to_state.object_id.split('_sensor_motion')[0].replace('_',' ')|capitalize %}
            Alarm triggered in the {{room}}
          message: >
            {% set room = trigger.to_state.object_id.split('_sensor_motion')[0].replace('_',' ')|capitalize %}
            {{as_timestamp(now())|timestamp_custom('%X')}}: {{room}} triggered the alarm, check whats up!

thanks!

First of all, while not wrong, your formatting of templates bothers me lol. Single line templates should just be wrapped in quotes. You’re using the multi line symbol for a single line. You can continue to do that, I just wasn’t sure if you realized you could do single line templates another way.

entityid: "{{trigger.entity_id}}"

Next, I’m not sure how this ever worked. “{{trigger.entity_id}}” has always been a string before 0.118. In no world would this have ever returned anything other than a string…because templates could ONLY return strings. Thus “trigger.entity_id.object_id” would have always failed.

If you just want the object id, pass in the object id instead rather than try to parse it in the script. Or even better, just use the friendly name.

      - service: script.alarm_triggered
        data:
          oid: "{{trigger.to_state.object_id}}"
          parsed_name: "{{ trigger.to_state.object_id.split('_sensor_motion'[0].replace('_','') | capitalize }}"
          # state.name will be the friendly_name if set. If not set, it will be the object_id. 
          name: "{{ trigger.to_state.name}}"
  alarm_triggered:
    alias: Alarm triggered
    mode: queued
    variables:
      time_now: "{{ as_timestamp(now()) | timestamp_custom('%X')}}"
    sequence:
      service: notify.mobile_app_calltheboss
      data:
        title: "Alarm triggered in the room {{parsed_name}}"
        message: "{{time_now}}: {{name}} triggered the alarm, check what's up!"

You can find out what state objects are available to you here.

I realize this doesn’t answer your original question as to “why is this broken now”? I don’t know HOW it worked before, so I can’t begin to say why it’s broken now.

In all of your other examples, everything looks completely normal. You’re passing a string, and parsing a string. You weren’t trying to pass a whole state_object and parse that on the other side.

yeah, thanks, I am aware of the State objects (and of the multiline templates. I always use these to prevent the need for outer quoting)

You’re right about the friendly_name, that is easier, and more to the point it this particular case.

Just that in the presence automation I can use

{{state_attr(entityid,'friendly_name')}}

while I can’t use that in this alarm automation, where the exact same variable is passed to the script.

and, as displayed in the bottom automation, I can use the object_id of the trigger.to_state, so that might be even more to the point. NO extra script, just a simple service.

(liked the script though, because it can be called in other automations too.)

thanks for helping me out here. appreciated,