If-Then-Else templating

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'
          .......

Try

condition: template
value_template: "{{ states('counter.zone' + trigger.to_state.state|string)|int(0) == 0}}"

Because the entity_id option of a State Condition doesn’t support templates.

Use the Template Condition provided by msp1974 or this slightly simpler version:

    action:
      - if:            
          - condition: template
            value_template: "{{ states('counter.zone' ~ trigger.to_state.state) == '0' }}"

Hi Mark,

Thanks a lots… guess that helps to solve my issue with a switch in the order of ‘int(0) & string position. Appreciate very much your spontaneous and kind help.

- condition: template
  value_template: "{{ states('counter.zone' +  trigger.to_state.state | int()|  string)  ==  '0' }}"

Hi Taras,

Thanks also for pointing and helping out the issue.

I tried using your template as well… as the output return some constant result not affected by the condition. Ii tried checking “{{ states(‘counter.zone’ ~ trigger.to_state.state ) }}” seems to return ‘unknown’. I tried adding ‘ |int()|string ‘ since the ‘trigger.to_state.state’ returns 1 decimal point. But that doesn’t help… so i end up using Mark’s suggestion with slight switch in the string & int position. Thanks so much for your suggestions and help.

Had that important detail been mentioned, I would have included the int filter.

The string filter isn’t required. The tilde operator (~) performs a proper string concatenation.

- condition: template
  value_template: "{{ states('counter.zone' ~  trigger.to_state.state | int(0))  ==  '0' }}"