Automation with choose: use a IKEA tradfri toggle to call a script

Hello there.

I wrote my first YAML automation and as asected, it doesn’t works. What we have:

-HA CORE version 2022.4 installed via docker on a raspberry;

  • a switch: Vimar 14592 connected via Z2M;
  • a script that turns on the switch, created using HA GUI:
accendi_bagno_matrimoniale:
  alias: Accendi bagno matrimoniale
  sequence:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.luce_bagno_matrimoniale
  mode: single
  • a script that turns off the switch, created using HA GUI:
 spegni_bagno_matrimoniale:
   alias: Spegni bagno matrimoniale
   sequence:
   - service: switch.turn_off
     data: {}
     target:
       entity_id: switch.luce_bagno_matrimoniale
  mode: single

What i’m trying to do is to write just one automation that works as one, instead to have 2 scripts.

SO this is my try:

#GESTIONE LUCI BAGNO
- alias: "Gestione luce bagno matrimoniale"
  description: "Gestione della luce"
  trigger:
    - platform: device
      domain: mqtt
      type: action
      subtype: toggle
      device_id: 762c04eec87724332811cee444a0e48a
  action:
     - choose:
       - conditions:
           - condition: device     
             entity_id: switch.luce_bagno_matrimoniale
             device_id: 0aaff95053b11210959404527cd269e2
             type: is_off
         sequence:
           - service: script.accendi_bagno_matrimoniale
       default:
         - service: script.spegni_bagno_matrimoniale

But it doesn’t works. YAML seems to be ok ,but HA returns this error:

Logger: homeassistant.config
Source: config.py:454
First occurred: 11:50:43 (1 occurrences)
Last logged: 11:50:43

Invalid config for [automation]: required key not provided @ data['action'][0]['choose'][0]['conditions'][0]['domain']. Got None. (See ?, line ?).

I tried to edit the file but this is the best that I can do at the moment… any suggestions?
Thanks. Please let me now if you need other informations.

You don’t need a choose action or the scripts. There is a switch toggle service that will switch off if on and vice-versa.

- alias: "Gestione luce bagno matrimoniale"
  description: "Gestione della luce"
  trigger:
    - platform: device
      domain: mqtt
      type: action
      subtype: toggle
      device_id: 762c04eec87724332811cee444a0e48a
  action:
    - service: switch.toggle
      target:
        entity_id: switch.luce_bagno_matrimoniale

I tried with your code, HA give me an error:

Logger: homeassistant.config
Source: config.py:454
First occurred: 18:19:57 (1 occurrences)
Last logged: 18:19:57

Invalid config for [automation]: required key not provided @ data['discovery_id']. Got None. (See ?, line ?)

That’s due to your trigger.

Thanks for the hint.
I found this and i solve the problem.

https://www.zigbee2mqtt.io/guide/usage/integrations/home_assistant.html#controlling-zigbee2mqtt-via-home-assistant