Automation value_template match 2 conditions?

Hi.

I will do an automation wit 2 conditions from zigbee2mqtt
 zigbee2mqtt:info 11/9/2019, 6:50:10 PM MQTT publish: topic 'zigbee2mqtt/remote', payload '{"linkquality":39,"last_seen":"2019-11-09T17:50:10.385Z","action":"on","action_group":146}'

I have this

- alias: "remote lamp staand on"
  trigger:
   - platform: mqtt
     topic: 'zigbee2mqtt/remote'
  condition:
     condition: template
     value_template: '{{ "on" == trigger.payload_json.action }}'
  action:
     service: light.turn_on
     data:
       entity_id:
         - light.staandelamp

How can I make an and statement?

 value_template: '{{ "on" == trigger.payload_json.action }}'  and '{{ "146" == trigger.payload_json.action_group }}'

Easiest way is just to have two conditions.

Agreed, since multiple conditions are evaluated with “and”. But another way to do it is to simply put the “and” in the statement:

 value_template: '{{ "on" == trigger.payload_json.action and "146" == trigger.payload_json.action_group }}'

Hi thnx for the replay but I tried these

- alias: "remote lamp staand on"
  trigger:
   - platform: mqtt
     topic: 'zigbee2mqtt/remote'
  condition:
     condition: template
     value_template: '{{ "on" == trigger.payload_json.action }}'
     condition: template
     value_template: '{{ "146" == trigger.payload_json.action_group }}'
  action:
     service: light.turn_on
     data:
       entity_id:
         - light.staandelamp

and these

- alias: "remote lamp staand on"
  trigger:
   - platform: mqtt
     topic: 'zigbee2mqtt/remote'
  condition:
     condition: template
     value_template: '{{ "on" == trigger.payload_json.action and "146" == trigger.payload_json.action_group }}'
  action:
     service: light.turn_on
     data:
       entity_id:
         - light.staandelamp

But they both did not work.

I noticed that the 146 value is not between quotes in de message. Could this be the issue?

zigbee2mqtt:info 11/9/2019, 6:50:10 PM MQTT publish: topic 'zigbee2mqtt/remote', payload '{"linkquality":39,"last_seen":"2019-11-09T17:50:10.385Z","action":"on","action_group":146}'

You need to have dashes before each condition like under trigger.

like this?

- alias: "remote lamp staand on"
  trigger:
   - platform: mqtt
     topic: 'zigbee2mqtt/remote'
  condition:
   - condition: template
     value_template: '{{ "on" == trigger.payload_json.action and "146" == trigger.payload_json.action_group }}'
  action:
   - service: light.turn_on
     data:
       entity_id:
         - light.staandelamp

Only if you have more than one condition is it necessary, but it works for one as well.

Which programming language(s) are you familiar with?

I’m curious to learn which one taught you to adopt this style of test:

if constant is equal to variable

as opposed to the more widely used:

if variable is equal to constant

There something Yoda-like about “on is the variable”.

1 Like

I got used to the “Yoda” syntax because it’s not as easy to accidentally leave out an ‘=’ and end up with an assignment instead of a comparison…in C/C++ anyway.

Certainly not as easy to read though.

I should’ve been more specific because my question was directed at kroonen because the Yoda-style appears in the first post.

Nevertheless, that’s an interesting reason for reversing the order.

It still not working. it looks like the action_group doesn’t work. Any idea what goes wrong

This is the message that is send. Action on is working , But if I try only action_group, nothning happens.

Nov 15 20:26:58 zigbee2mqtt npm[15818]: zigbee2mqtt:info  2019-11-15T19:26:58: MQTT publish: topic 'zigbee2mqtt/remote', payload '{"linkquality":36,"last_seen":"2019-11-15T19:26:58.107Z","action":"on","action_group":146}'
- alias: "remote lamp staand on"
  trigger:
   - platform: mqtt
     topic: 'zigbee2mqtt/remote'
  condition:
#   - condition: template
#     value_template: '{{ "on" == trigger.payload_json.action }}'
   - condition: template
     value_template: '{{ "146" == trigger.payload_json.action_group }}'
  action:
   - service: light.turn_on
     data:
       entity_id:
         - light.staandelamp

I think it’s because 146 is an integer in that JSON, not a string, so if you take the quotes off of the number, it may work.

That worked, thx