must be valid. i.e you cannot leave the else empty to do nothing.
It looks like in cases where there genuinely is never (ha! ha!) gong to be an else condition the suggested way round it is to have a dummy script which does something .
Has anyone found another cleaner way to get around this and if not what do you put in your dummy script (or is there a trivial and inconsequential service that could called)?
For example:
- service_template: >
{% if (now().month == 1 and now().day == 2) or
(now().month == 3 and now().day == 4) %}
script.birthday_greeting
{% else %}
{% endif %}
Typically, I use a condition prior to the service template. But remember, if you use a condition, all services beyond the condition will not run. So in your example, this is what it owuld look like:
- condition: template
value_template: >
#Easily expandable. just add ", (month, day)" inside square brackets.
{% set birthdays = [ (1, 2), (3, 4) ] %}
{% set today = ( now().month, now().day ) %}
{{ today in birthdays }}
#everything beyond this point will not run if the previous condition hasn't been met.
- service: script.birthday_greeting
I agree but do you have an alternative for cases where there really isn’t an else but there are further steps in the automation?
The only other way I have come up with is to always call a separate script which performs one function and let that script have the condition. Probably slightly more correct but in my opinion slightly less maintainable. YAML is bad enough without having tiny bits of code all over the place just to deal with a logic problem.
Agreed. Tonight I have actually been able to remove it from the few templates I was using it in by using either David’s suggestion to put the last option in the else case or petro’s condition suggestion. e.g.
condition:
- condition: template # Only if currently running any heat mode
value_template: "{{ 'Heat' in states.input_select.lounge_ac_mode.state }}"
action:
service_template: >
{% if is_state('input_select.lounge_ac_mode', 'Powerful Heat') %} shell_command.lounge_ac_powerful_heat
{% elif is_state('input_select.lounge_ac_mode', 'Normal Heat') %} shell_command.lounge_ac_normal_heat
{% elif is_state('input_select.lounge_ac_mode', 'Silent Heat') %} shell_command.lounge_ac_silent_heat
{% endif %}