The automation is having the possibility named: Choose. So you can add option + action, option+action. This is basically an if/else composed with 2 if.
You can do, it’s entirely your choice. I prefer templates to choose where possible because it makes far less code, and is much neater.
Hello Mf_Social et. al
I was going through your series of mails to apply to a problem that I am facing with the template alarm panel. The issue that I am trying to solve is in the service section of the template, where I want to use the door sensor state to
prevent the arming of the panel if it is left open (on). If it the door is closed (off), then I want the arming to proceed to either arm_home or arm_away with a preset-code/pin.Preformatted text
alarm_control_panel:
- platform: template
panels:
safe_alarm_panel:
value_template: "{{ states('alarm_control_panel.alarm') }}"
unique_id: 5f6ef76cc48
arm_home:
- service: >
{% if is_state('binary_sensor.garage_entry_door', 'off') %}
alarm_control_panel.alarm_arm_home
target:
entity_id: alarm_control_panel.alarm
data:
code: !secret alarm_control_panel_code
{% else %}
persistent_notification.create
data:
notification_id: "377"
message: "Alarm cannot be armed since doors are open"
title: "Alarm"
{% endif %}
I have only shown the actions for the first state arm_home above.
The hass interpreter is giving an error as follows:
2021-02-21 19:37:25 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while scanning for the next token
found character ‘%’ that cannot start any token
in “C:\Users\rsiva\Documents\Hass\HassWP_2021.1.1\config\configuration.yaml”, line 38, column 14
// Column 14 of line 38 is the place where {% starts below the service token.
I am not sure if the way I have coded is correct or something wrong.
If you can throw some suggestions, it will help
PS: The template alarm panel is used as front end interface to the real alarm panel as a function of the user selected function (arm_home, arm_away, disarm) and the sensor states.
You can only template values, you’re trying to template entire services.
You’ll need to find another way to achieve this.
mf_social
Ok. Thanks for the comments. I will see how else to model my situation and make the code work differently.
Thanks
rsiva-tech
I would imagine you can achieve what you want using the choose function. I have managed similar complex ‘decision’ making automations with it myself.
Sparky
Really appreciate the pointer to the choose function. The lack of a good documentation on the possible features in HA is what is making the life of newbies difficult. But this forum with experts has been awesome. Would you mind cutting and pasting portion of your code with “choose” as a working example.
Thanks
rsiva tech
No problem. Here is a basic choose function:
- choose:
- conditions:
- condition: state
entity_id: sun.sun
state: above_horizon
sequence:
- service: switch.turn_off
entity_id: switch.circadian_lighting_passage_circadian_lighting
- service: light.turn_on
data:
entity_id: light.passage_lights
white_value: 255
default:
- service: switch.turn_on
entity_id: switch.circadian_lighting_passage_circadian_lighting
- service: light.turn_on
entity_id: light.passage_lights
Used in conjunction with the right conditions (and/or’s etc.) you can make things really powerful.
Sparky
Kudos for posting the sun example for the choose function and how the if then else is structured to carry out a sequence of actions. I will apply this to my problem later when I get time to revamp the code.
Thanks again
rsiva-tech
arm_home:
- choose:
- conditions:
- condition: state
entity_id: binary_sensor.garage_entry_door
state: "off"
sequence:
- service: alarm_control_panel.alarm_arm_home
target:
entity_id: alarm_control_panel.alarm
data:
code: !secret alarm_control_panel_code
default:
- service: persistent_notification.create
data:
notification_id: "377"
message: "Alarm cannot be armed since doors are open"
title: "Alarm"
I got the choose function to work in my first attempt without syntax error.
In the notification window, got the message I was looking for in the simulated test case .
Alarm cannot be armed since doors are open
Thanks to the HA community for edifying the usage model of “choose”
rsiva-tech
I have been on this platform for some time this is by far the best explanation I have read on the indentation concept
I am not much fan of using jinja templating for conditions if the simple yaml definition in HA has a “choose” option, which is basically a switch that can be used for if/else conditions.
I recently wrote the first script for the remote car start routine (open garage door IF not yet opened, then start the car from remote key)
In pseudo code it would do this:
if door.state == 'closed':
service.door.open
wait for door to be opened
service.button.start_car
Actual yaml:
get_car_ready:
alias: Get car ready
icon: mdi:car-lifted-pickup
mode: single
sequence:
- choose:
- alias: IF door is closed -> open it first
conditions:
- condition: state
entity_id: sensor.garage_door_state
state: closed
sequence:
- service: system_log.write
data:
message: Opening garage door
level: info
- service: cover.open_cover
target:
entity_id:
- cover.garage_door
- wait_template: '{{ is_state(''sensor.garage_door_state'', ''open'') }}'
- service: button.press
target:
entity_id: button.start_engine
- delay: 10
- service: notify.notify
data:
message: Chevy Avalanche started
for if/else condition just use the default
option in choose. Documentation even explains this process:
The
choose
action can be used like an “if” statement. The firstconditions
/sequence
pair is like the “if/then”, and can be used just by itself. Or additional pairs can be added, each of which is like an “elif/then”. And lastly, adefault
can be added, which would be like the “else.”
EDIT: May 2022
As of HA 2022.5.* you have an option to actually use if…else conditions and for loop in yaml. No need to use the choose any more.