If/else automatisation mqtt switch using object_id/unique_id or payout conditions

Hi, I have 2 automatisations and I would like only one with an IF/ELSE

this is one of all mqtt switch :

mqtt:
  switch:
    - name: "Serveur NGINX"
      object_id: server_nginx
      unique_id: nabaztag_ztk3o7_nginx
      state_topic: "nabaztag/sensor/box-nabaztag/service_nginx/status"
      command_topic: "nabaztag/switch/box-nabaztag/service_switch"
      payload_on: "start_service_nginx"
      payload_off: "stop_service_nginx"
      state_on: "online"
      state_off: "offline"
      icon: "mdi:web"
      qos: 0
      retain: false
      device:
        identifiers:
          - nabaztag_box_ztk3o7

and my automatisations (on automations.yaml) :

## Nabaztag Service Switch
- alias: Nabaztag Service Switch MQTT
  trigger:
    platform: mqtt
    topic: nabaztag/switch/box-nabaztag/service_switch
  condition: "{{ trigger.payload in ['start_service_nginx', 'stop_service_nginx', 'start_service_sql', 'stop_service_sql', 'start_service_php', 'stop_service_php'] }}"
  action:
    - service: script.nabaztag_service
      data_template:
        action: "{{ trigger.payload }}"
        context_id: "{{ this.context.id }}"
- alias: Nabaztag Service Switch HTTP
  trigger:
    platform: mqtt
    topic: nabaztag/switch/box-nabaztag/service_switch
  condition: "{{ trigger.payload not in ['start_service_nginx', 'stop_service_nginx', 'start_service_sql', 'stop_service_sql', 'start_service_php', 'stop_service_php'] }}"
  action:
    - service: rest_command.nabaztag_httprequest
      data_template:
        action: "{{ trigger.payload }}"
        payload: '{"context_id": "{{ this.context.id }}"}'

So I have 2 automatisations and I would like only one with an IF/ELSE

And it would be even better if the condition could be a list of object_id or, failing that, a unique_id list

object_id list :

  • service_php
  • server_nginx
  • server_sql

unique_id list :

  • nabaztag_ztk3o7_php
  • nabaztag_ztk3o7_nginx
  • nabaztag_ztk3o7_sql

Everything I put works, but I would like something cleaner

and on configuration.yaml (bonus ^^)

rest_command:
  nabaztag_httprequest:
    url: 'http://nabaztag.local/api/{{ action }}'
    method: 'post'
    payload: '{{ payload | tojson }}'
    content_type: 'application/json'

Thanks for your help

finally I found the solution myself ^^ thanks to chatGPT and Bing for the little help even if they talk a lot of nonsense

## Nabaztag Service Switch
- alias: Nabaztag Service Switch
  trigger:
    platform: mqtt
    topic: nabaztag/switch/box-nabaztag/service_switch
  action:
    - variables:
        # trigger.payload = (start|stop)_service_XXX
        service_name: "{{ trigger.payload.split('_')[2] }}" # get XXX
        http_ready: "{{ states('switch.server_nginx') }}"
        mqtt_ready: "{{ states('switch.service_mqtt') }}"
    - choose:
      # http (nabaztag_httprequest) or mqtt (nabaztag_service)
      - conditions: "{{ http_ready == 'on' and service_name not in ['nginx','sql','php'] }}"
        sequence:
          - service: rest_command.nabaztag_httprequest
            data_template:
              action: "{{ trigger.payload }}"
              payload: |-
                {
                  "context_id": "{{ this.context.id }}",
                  "service": "{{ service_name }}",
                  "http": "{{ http_ready }}",
                  "mqtt": "{{ mqtt_ready }}",
                }    
      - conditions: "{{ (http_ready == 'off' and service_name != 'mqtt') or service_name in ['nginx','sql','php'] }}"
        sequence:
          - service: script.nabaztag_service
            data_template:
              action: "{{ trigger.payload }}"
              context_id: "{{ this.context.id }}

with that is perfect, priority is on http request, when the http server is down priority is on mqtt service. And it’s imposible to stop AND mqtt service AND http server. So the system can’t be blocked.

it’s perfect, I’m so happy hahahahha ^^