jwelter
(John Welter)
June 7, 2018, 11:44am
1
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"
petro
(Petro)
June 7, 2018, 11:55am
2
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.
petro
(Petro)
June 7, 2018, 12:02pm
3
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
jwelter
(John Welter)
June 7, 2018, 12:38pm
4
Thanks for this! Two follow on questions:
Is the .state in the is_state a typo? Pasting into template editor seems to indicate it’s not required.
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 }}
petro
(Petro)
June 7, 2018, 1:32pm
5
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
jwelter
(John Welter)
June 7, 2018, 3:49pm
6
Thanks, I can easily flip the logic as you suggest in these cases. Thanks for this help!
jwelter
(John Welter)
June 7, 2018, 6:20pm
7
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.
petro
(Petro)
June 7, 2018, 8:56pm
8
do you have a log for the errors? I can help you trouble shoot them
jwelter
(John Welter)
June 7, 2018, 10:09pm
9
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