Question on service_template

I have a couple places where I want to check a input_boolean to make a decision to execute a script or not.

I was putting the condition in the script and calling it from the main automation; and let the script either execute or fail. But to me it’s a bit messy as these scripts are always executed to just terminate on the condition. Plus the conditions are not visible from the calling level that makes maintenance a bit challenging.

So looking to use service_template. Is this valid, as the data_template is dangling:

 - service_template: >-
        {%- if is_state('input_boolean.notify_doors.state', 'on') -%}
          script.notify_ios_engine
        {%- endif -%}
   data_template:
        message: 'EXIT DOOR: {{ trigger.to_state.attributes.friendly_name }} to {{ trigger.to_state.state | replace("on","open") | replace("off","closed") }}'
        who: "family"

This will error out when is_state(‘input_boolean.notify_doors.state’, ‘on’) condition is not met. Services require a service. Giving nothing to a service call will result in an error.

If you want to always check for a message, make a script with variables:

notify script:

send_notification:
  sequence:
    - condition: template
      value_template: "{{ notify }}"
    - service: script.notify_ios_engine
      data_template:
        message: "{{ message }}"
        who: "{{ who }}"

Then in every action that you want a notify:

- service: script.send_notification
  data_template:
    message: 'EXIT DOOR: {{ trigger.to_state.attributes.friendly_name }} to {{ trigger.to_state.state | replace("on","open") | replace("off","closed") }}'
    who: 'family'
    notify: "{{ is_state('input_boolean.notify_doors.state', 'on') }}"
1 Like

Thanks for this! Two follow on questions:

  1. Is the .state in the is_state a typo? Pasting into template editor seems to indicate it’s not required.

  2. If I don’t always want to pass notify to the script as for some notifications it’s always sent can I do something like this in the script?

 - condition: template
   value_template: >-
       {%- if notify is not string -%}{%- set notify = 'True' -%}{%- endif -%}
         {{ notify }}

Yes, typo.

If you flip the logic, you could have this ability without changing the value template much.

send_notification:
  sequence:
    - condition: template
      value_template: "{{ not keep_quiet }}"
    - service: script.notify_ios_engine
      data_template:
        message: "{{ message }}"
        who: "{{ who }}"
- service: script.send_notification
  data_template:
    message: 'EXIT DOOR: {{ trigger.to_state.attributes.friendly_name }} to {{ trigger.to_state.state | replace("on","open") | replace("off","closed") }}'
    who: 'family'
    keep_quiet: "{{ is_state('input_boolean.notify_doors.state', 'off') }}"

Then, if you don’t populate keep_quiet, it should naturally fill it with ‘None’, which is equal to false. So not false is true, and you’d get your notifications without specifying it.

1 Like

Thanks, I can easily flip the logic as you suggest in these cases. Thanks for this help!

This doesn’t seem to work. I added some logging and see the False being passed into the script but condition template is not working.

I suspect it’s being passed as a string versus a state or something causing the template not to evaluate.

do you have a log for the errors? I can help you trouble shoot them

Got it working as follows:

 - service: script.notify_ios_engine
      data_template:
        message: >-
          EXIT DOOR: {{ trigger.to_state.attributes.friendly_name }} to {{ trigger.to_state.state | replace("on","open") | replace("off","closed") }}
        who: "family"
        notify: "{{ states('input_boolean.notify_doors') }}"


  - condition: template
        value_template: >-
          {%- if notify is not string -%}{%- set notify = 'on' -%}{%- endif -%}
            {{ notify == 'on' }}
.... rest of script....
1 Like