jchh
((not John))
October 12, 2023, 10:25am
1
Hi,
I have an automation that works fine, but am playing with condition template shorthand as part of my education.
Can someone tell me why the 2 hashed out conditions don’t work whereas the ‘normal’ numeric_state
is fine?
automation:
- alias: "Tapo: Auto on:off"
id: tapo_on_off
mode: single
trigger:
- platform: state
entity_id: input_select.house_mode
- platform: state
entity_id: zone.home
condition: []
action:
- if:
# - condition: "{{ states('zone.home') != '0 }}"
# - condition: "{{ is_state('zone.home','1') }}"
- condition: numeric_state
entity_id: zone.home
above: 0
- condition: state
entity_id: input_select.house_mode
state: awake
then:
- service: switch.turn_on
target:
entity_id: switch.tapo_camera_privacy
- service: script.turn_on
target:
entity_id: script.tapo_notify_status
else:
- service: switch.turn_off
target:
entity_id: switch.tapo_camera_privacy
- service: script.turn_on
target:
entity_id: script.tapo_notify_status
jchh
((not John))
October 12, 2023, 10:54am
2
…is it that you simply can’t use shorthand for if/then conditions?
starob
October 12, 2023, 11:45am
3
Did you miss a quote after the “0”?
1 Like
Dujith
(Charles Evers)
October 12, 2023, 12:12pm
4
Both of them work fine (if you corrected the first line like
@starob says)
The Dev Tools are a great way to test these kind of thing to check if you coded them correctly.
And i dont believe if
works like that in automations. In the action you use the if
with a condition
, that condition
can be a template:
action:
- if:
- condition: template
value_template: "{{ states('zone.home') != '0' }}"
then:
1 Like
123
(Taras)
October 12, 2023, 12:45pm
5
Based on the examples in the linked documentation, change the format of this:
- condition : "{{ states('zone.home') != '0' }}"
To this:
- "{{ states('zone.home') != '0' }}"
The leading condition:
is needed only when the (shorthand) Template Condition is within a sequence of actions:
action:
- service: light.turn_on
target:
entity_id: light.kitchen
- condition: "{{ states('sensor.kitchen_light_level') | int(0) < 50 }}"
- service: switch.turn_on
target:
entity_id: switch.undercabinet
3 Likes
jchh
((not John))
October 12, 2023, 1:06pm
7
I did - thanks for spotting - but it was just a typo in my post. My original had it correct and it still didn’t work.
jchh
((not John))
October 12, 2023, 1:12pm
8
That was just a typo in my post - sorry for the confusion. I did have them working in dev tools (per your picture) before I posted the question - that was why I posted the question.
Taras was correct though; my mistake was in having condition:
in front of the template, so the following works fine:
automation - alias: "Tapo: Auto on:off"
id: tapo_on_off
mode: single
trigger:
- platform: state
entity_id: input_select.house_mode
- platform: state
entity_id: zone.home
condition: []
action:
- if:
- "{{ states('zone.home') != '0' }}"
- condition: state
entity_id: input_select.house_mode
state: "Awake"
then:
- service: switch.turn_on
target:
entity_id: switch.tapo_camera_privacy
- service: script.turn_on
target:
entity_id: script.tapo_notify_status
else:
- service: switch.turn_off
target:
entity_id: switch.tapo_camera_privacy
- service: script.turn_on
target:
entity_id: script.tapo_notify_status
1 Like