Passing data to service template automation


I have 2 scripts which are called depending on states of two sensors. The scripts require input data (as variables). The scripts work fine when called via both developer tools and via automation without service template. But, it doesn’t work with service template. The service template itself gets chosen correct which tells me the template conditions are okay. But, script isn’t called.

alias: "Cam:"
  - platform: mqtt
    topic: bi/alert/motion/main
      cam: "{{ trigger.topic.split('/')[3] }}"
  - condition: template
    value_template: "{{ trigger.payload_json.trigger == \"ON\" }}"
  - service: >
      {% if states('device_tracker.something') == 'home' and states('sun.sun') ==
      'below_horizon' %}
      {% elif states('device_tracker.something') == 'home' and states('sun.sun')
      == 'above_horizon' %}
      {% else %}
      {% endif %}
        cam_short_name: "{{ cam }}"
        cam_name: "{{ }}"
        cam_fire_time: "{{ trigger.payload_json.time }}"
        cam_rec_link: "{{ }}"
mode: single

The data variables are created correctly (seen from error log below). So, it seems it the combination of service and data fields (which becomes variables for the scripts) need the right formatting?


Error: Template rendered invalid service: script.A data: cam_short_name: "main" cam_name: "front_main" cam_fire_time: "2023-10-18T15:45:16.085Z" cam_rec_link: "@19289829960501"

Two issues.

  1. entity ids are lower case. So script.a not script.A.

  2. The sun can only be above or below the horizon so there is no point testing for this if you are calling script.a in both cases. Get rid of the extraneous sun test.

Hello @tom_l
Thank you for taking the time to respond.

  1. noted but a non-issue. I changed the scriptname from <actual> to A for brevity whilst posting here
  2. i have yet to implement the script which is to implement a different behavior for above_ vs below_ horizon. Hence, just a placeholder for now so that i don’t forget.

Thank you @tom_l for the help. The offender was > in service:. Changing it to | did the trick. Whodhavethunk~