Passing template values in variable to script from automation


Could some one point out what’s wrong with this automation … I get only a blank value passed on to the script

    - service: script.notify_kv_phone
        variables: >-
          {% set msg = "{{state_attr(trigger.to_state.entity_id, 'friendly_name') }} detected " %}
          {{ msg }}

Thanks in advance…

You’re putting templates inside templates. That’s not how templating works. The entire line is a chunk of code. There’s no reason to put the ‘template indicators’ inside a line with template indicators.

This is wrong:

{% {{ code }} %} 
{{ {% code %} }} 

This is correct:

{% code %} 
{{ code }} 

Also, you’re taking code and turning it into a string.

What variable are you expecting to receive in the script? In your case, you have no variable. variables is a special field for scripts to pass variables. You’re passing nothing essentially. Also, you technically don’t need the vairables field because it’s a bit redundant and it’s legacy functionality. You can just supply the variable itself to the data.

Lastly, data_template is no longer required as long as you’re using the newest version of Home Assistant.

So, assuming that you are trying to pass the msg variable:

    - service: script.notify_kv_phone
        msg: "{{ state_attr(trigger.to_state.entity_id, 'friendly_name') }} detected"

And as an alternative, you can get the friendly_name directly from the state object instead of using the state_attr function

    - service: script.notify_kv_phone
        msg: "{{ }} detected"

Thanks for pointing out the syntax errors. Good to learn as I’m exploring new territories.
However, I still do not the get the value ’ the ‘object_id’ which triggered the action, in the message to notify script.

Basically the trigger has multiple entities [ input_boolean.camera1, …2 …3 etc ] and I’m trying to send a notification, which one triggered the action.

Here is my full automation code:
sorry, I mentioned as input_boolean instead of binary_sensor… [makes no difference from the problem point of view…:)]


- alias: MEye Cams Motion Detected
  initial_state: true
    - platform: state
        - binary_sensor.cam1_motion
        - binary_sensor.cam2_motion
        - binary_sensor.cam3_motion
        - binary_sensor.cam4_motion
        - binary_sensor.cam5_motion
        - binary_sensor.cam6_motion
        - binary_sensor.cam7_motion
        - binary_sensor.cam8_motion
        - binary_sensor.cam9_motion

      to: 'on'

    - service_template: 'shell_command.clear_{{trigger.to_state.object_id}}'
    - service: script.turn_on
          - 'script.{{trigger.to_state.object_id}}_action'
          - 'script.show_{{trigger.to_state.object_id}}'

    - service: script.notify_kv_phone
        msg: "{{ state_attr(trigger.to_state.entity_id, 'friendly_name') }}"

The first 2 action service works. only the last one is not passing the value of the entity id name.


##  Testing Passed parameter..	

    - service: notify.kv_phone
        message: "{{msg}} detected"

I get only ‘detected’ notification without the camera name.

What version HA are you on?