Hi,
I am stuck trying to use a more flexible if-then-else condition with templates and trigger entities… the below code are just a simplified version of trying to check the logic of managing media players from one zone to another for zone synchronisation purposes. each players has an ‘input_number’ to record its current zone placement, each zone has a counter to keep track of the number of players currently is attached to it.
eg.
- ‘input_number.player1_zone_no’. - stores the zone number which player1 is attached to. so if it switch to zone 3, this helper will change to “3”. Any change to this input_number will trigger the zone synchronisation process which is in the simplified ‘If-then-else’ logic. this number will also be used to select the right zone counter… eg… ‘counter.zone3’ for conditional check
- counter.zone1 - the number of players that is currently inside that zone 1… eg… if counter.zone3 is ‘2’, means there are 2 players inside zone 3… if ‘0’, means the zone has no player current inside. As mentioned in the previous point, if player1 is changed to zone3, I need to be able to check the condition based on counter.zone(3).
I have checked and the code doesn’t seems to execute beyond the ‘conditional’ part. If I hard-code the entity_id to ‘counter.zone1’, for example, instead of using the ‘counter.zone{{trigger.to_state.state}}’, it will pass through correctly and execute the If-Then-Else… but I can’t be limited to zone1 in this case…I need to change to different counter.zone such as counter.zone1, counter zone2 …etc
I believe the issue is in the templating part of the condition statement. Please help. thanks
- alias: link media players to zone
description: "sync players to new zone"
mode: single
trigger:
platform: state
entity_id:
- input_number.player1_zone_no
- input_number.player2_zone_no
- input_number.player3_zone_no
action:
- if:
- condition: state
entity_id: "counter.zone{{ trigger.to_state.state }}"
state: "0"
then:
- service: persistent_notification.create
data:
message: " unlink player from old zone"
.......
else:
- service: persistent_notification.create
data:
message: " link player to new zone'
.......