Calling script from automation through template

What am I doing wrong here? I could of course split this into two automations, but it’s more fun to have it in one :slight_smile:

alias: Driveway Lamp On/Off
trigger:
- entity_id: binary_sensor.dark_outside
  platform: state
action:
  - data:
      message: >-
        Dark Outside changed state to {{ states("binary_sensor.dark_outside") }} because of a combination of these values: {{ '\n' -}}
        - Sun Elevation is {{ state_attr("sun.sun","elevation") }} {{ '\n' -}}
        - OWM Cloud Coverage is {{ states("sensor.owm_cloud_coverage") }}
    service: notify.mainplatform
  - service: >-
      {% if (is_state("binary_sensor.dark_outside","on")) -%}
        script.driveway_lamp_high
      {%- else -%}
        script.driveway_lamp_off
      {%- endif %}

The error I get is:

Invalid config for [automation]: Service {% if (is_state(“binary_sensor.dark_outside”,“on”)) -%}
script.driveway_lamp_high
{%- else -%}
script.driveway_lamp_off
{%- endif %} does not match format . for dictionary value @ data[‘action’][1][‘service’]. Got None. (See ?, line ?).

Since there is no service.execute which I could use, and then pass the name of the specific script to run in a data part, I’m a bit stuck here…

Nevermind, script.turn_on does the trick…

Until the next release that removes template magic you should be doing this:

action:
  - data_template:
      message: >-
        Dark Outside changed state to {{ states("binary_sensor.dark_outside") }} because of a combination of these values: {{ '\n' -}}
        - Sun Elevation is {{ state_attr("sun.sun","elevation") }} {{ '\n' -}}
        - OWM Cloud Coverage is {{ states("sensor.owm_cloud_coverage") }}
    service: notify.mainplatform
  - service_template: >-
      {% if (is_state("binary_sensor.dark_outside","on")) -%}
        script.driveway_lamp_high
      {%- else -%}
        script.driveway_lamp_off
      {%- endif %}
1 Like

Yup, so I turned (again?) into one of those people asking the same _template question :slight_smile:
Can’t wait for the magic to be gone :slight_smile: