Automation with multiple triggers

Hi.

I have an automations.yaml included from my configuration:

- id: GardenLightAllOff
  alias: Garden Light All Off
  trigger:
    - platform: mqtt
      topic: bridge/tele/RESULT
      value_template: "{{value_json.RfReceived.Data}}"
      payload: '20D713'
  action:
    - service: mqtt.publish
      data:
        topic: garden4ch/cmnd/power1
        payload: "off"
    - service: mqtt.publish
      data:
        topic: garden4ch/cmnd/power2
        payload: "off"
    - service: mqtt.publish
      data:
        topic: ch4_01/cmnd/power4
        payload: "off"
    - service: mqtt.publish
      data:
        topic: garageweather/cmnd/power
        payload: "off"
    - service: mqtt.publish
      data:
        topic: garagefront/cmnd/power
        payload: "off"
  mode: single

It works nicely. I’m assuming that GardenLightAllOff is an example of an automation (I’m not sure my terminology is correct), how can I trigger that sequence of lights off with another additional trigger, such as an NFC tag?

The question is twofold: first, I haven’t got my head around YAML and second, I’ve not tried running an action with a tag using YAML instead of the visual editor. I already have the tag programmed.

Many thanks!

Triggers are automatically “any”, so if you have a list on triggers and any of them match the automation will run:

- id: GardenLightAllOff
  alias: Garden Light All Off
  trigger:
    - platform: mqtt
      topic: bridge/tele/RESULT
      value_template: "{{value_json.RfReceived.Data}}"
      payload: '20D713'
    - platform: mqtt or some other platform
      # etc...

But with the information supplied that’s as much as I can help. For your NFC tag, is it configured in Home Assistant, how is it configured, does it present as a MQTT topic like your example or is it configured as some device?

Also the way you have your MQTT configured is very inefficient. Each of those topics could be configured as a template or a switch, then they can be manipulated via service calls and also have their state represented in the GUI.

I’d supposed “any” would be the default rather than “and”. I didn’t know I could simply list the triggers. Thank you.

Yes.

I’ve done “Add Tag” in the HA app, entered a name and pointed my phone at a new tag… The tag now appears in the list of tags. It’s been coded with some serialisation data, I imagine.

[quote=“zoogara, post:2, topic:476585, full:true”]
does it present as a MQTT topic like your example or is it configured as some device?[/quote]
No. It’s just been “added.” As I understand it, it’s nothing more than an event trigger.

I’m unsurprised. I wrote much of this about 4 years ago and it “just worked” so I left it alone. The recent change in how MQTT works prompted me to revisit the manual. My switches are VERY laggy (sometimes have to be hit several times). I’d like to understand how to improve them.

Now, you’re talking a foreign language!

Sometimes, I just need a little hook. A tiny prompt of what to search for. I have probably 30 examples of the above. What would it take to make these “efficient”? Is efficient a reduced amount of YAML or does it actually work internally in a better way?
Thanks again.

Have you considered using a Tag Trigger?

Tags are new to me as of this evening. I hadn’t considered it. Now I have, though!
I’m very rapidly figuring out that if I knew the answer, then I’d know what to search for!
Thanks.

Where do I start with the sensors??? I assume from the data that you are maybe using a Sonoff RF bridge or similar for 433Mhz devices? There are many ways to turn these into HA entities, I use a Python script to translate the codes into meaningful topics, which I then added as sensors to HA.

This means you can assign attributes to them and create entities in HA that you refer to rather than the MQTT data.

The Python script looks like:

d = { 
      '0BFB0A':['433mhz/sensor_front_door','ON','true'],
      '0BFB0E':['433mhz/sensor_front_door','OFF','true'],
      '19190E':['433mhz/switch_lounge_light','ON','true'],
      '19190A':['433mhz/switch_lounge_light','OFF','true'],
      '0E5B0E':['433mhz/switch_verandah_light','ON','true'],
      '0E5B0A':['433mhz/switch_verandah_light','OFF','true'],
      '4D300E':['433mhz/switch_side_verandah_light','ON','true'],
      '4D300A':['433mhz/switch_side_verandah_light','OFF','true'],
      '189A0E':['433mhz/switch_hall_light','ON','true'],
      '189A0A':['433mhz/switch_hall_light','OFF','true'],
      '0EEA0E':['433mhz/switch_office_light','ON','true'],
      '0EEA0A':['433mhz/switch_office_light','OFF','true'],
      '191907':['433mhz/button_lounge_light','ON','true'],
      'F6289E':['433mhz/sensor_hall_pir','ON','true'],
      '0E5B07':['433mhz/button_verandah_light','ON','true'],
      '46BB0E':['433mhz/switch_pergola_light','ON','true'],
      '46BB0A':['433mhz/switch_pergola_light','OFF','true'],
      '46BB07':['433mhz/button_sensor_light','ON','true'],
      'F9600A':['433mhz/sensor_west_front_window','ON','true'],
      'F9600E':['433mhz/sensor_west_front_window','OFF','true'],
      '92A90A':['433mhz/sensor_lounge_door','ON','true'],
      '92A90E':['433mhz/sensor_lounge_door','OFF','true'],
      '0F400A':['433mhz/sensor_east_front_window','ON','true'],
      '0F400E':['433mhz/sensor_east_front_window','OFF','true'],
      '14760A':['433mhz/sensor_east_sunroom_door','ON','true'],
      '14760E':['433mhz/sensor_east_sunroom_door','OFF','true'],
      'DDDE0A':['433mhz/sensor_back_door','ON','true'],
      'DDDE0E':['433mhz/sensor_back_door','OFF','true'],
      'A0020A':['433mhz/sensor_office_window','ON','true'],
      'A0020E':['433mhz/sensor_office_window','OFF','true'],
      '0BFB06':['433mhz/battery_front_door','ON','true'],
      'F96006':['433mhz/battery_west_front_window','ON','true'],
      '92A906':['433mhz/battery_lounge_door','ON','true'],
      '0F4006':['433mhz/battery_east_front_window','ON','true'],
      '147606':['433mhz/battery_east_sunroom_door','ON','true'],
      'DDDE06':['433mhz/battery_back_door','ON','true'],
      'A00206':['433mhz/battery_office_window','ON','true'],
      '191906':['433mhz/battery_lounge_light','ON','true'],
      '0E5B06':['433mhz/battery_verandah_light','ON','true'],
      '4D3006':['433mhz/battery_side_verandah_light','ON','true'],
      '46BB06':['433mhz/battery_pergola_light','ON','true'],
      '189A06':['433mhz/battery_hall_light','ON','true'],
      '0EEA06':['433mhz/battery_office_light','ON','true'],
      'E1BE0A':['433mhz/sensor_shed_door','ON','true'],
      'E1BE0E':['433mhz/sensor_shed_door','OFF','true'],
      'E1BE06':['433mhz/battery_shed_door','ON','true'],
      'F5577E':['433mhz/sensor_shed_bench_pir','ON','true'],
      '49580E':['433mhz/switch_garage_light','ON','true'],
      '49580A':['433mhz/switch_garage_light','OFF','true'],
      '495806':['433mhz/battery_garage_light','ON','true'],
      '0DCA0A':['433mhz/sensor_garage_door','ON','true'],
      '0DCA0E':['433mhz/sensor_garage_door','OFF','true'],
      '0DCA06':['433mhz/battery_garage_door','ON','true'],
      'E5E00A':['433mhz/sensor_shed_roller_door','ON','true'],
      'E5E00E':['433mhz/sensor_shed_roller_door','OFF','true'],
      'E5E006':['433mhz/battery_shed_roller_door','ON','true'],
      '0A4C81':['fob/remote','AWAY','true'],
      '0A4C82':['fob/remote','DISARM','true'],
      '0A4C84':['fob/remote','HOME','true'],
      '0A4C88':['fob/remote','SOS','true'],
      'DA6881':['fob/remote','AWAY','true'],
      'DA6882':['fob/remote','DISARM','true'],
      'DA6884':['fob/remote','HOME','true'],
      'DA6888':['fob/remote','SOS','true']
        }

p = data.get('payload')

if p is not None:
  if p in d.keys():
    service_data = {'topic':'{}'.format(d[p][0]), 'payload':'{}'.format(d[p][1]), 'qos':0, 'retain':'{}'.format(d[p][2])}
  else:
    service_data = {'topic':'433mhz/unknown', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
    logger.warning('<bridge_demux> Received unknown RF command: {}'.format(p))
  hass.services.call('mqtt', 'publish', service_data, False)

Triggered by this automation:

- alias: bridge_demultiplexer
  trigger:
  - platform: mqtt
    topic: tele/bridge/RESULT
  action:
  - service: python_script.bridge_demux
    data_template:
      payload: '{{trigger.payload_json.RfReceived.Data}}'

And an example of the sensors:

      - name: "Pergola Light Switch"
        state_topic: "433mhz/switch_pergola_light"
        payload_on: "ON"
        payload_off: "OFF"
    
      - name: "Back Sensor Light Button"
        state_topic: "433mhz/button_sensor_light"
        payload_on: "ON"
        off_delay: 5
    
      - name: "West Front Window"
        state_topic: "433mhz/sensor_west_front_window"
        payload_on: "ON"
        payload_off: "OFF"
        device_class: window

      - name: "Lounge Door"
        state_topic: "433mhz/sensor_lounge_door"
        payload_on: "ON"
        payload_off: "OFF"
        device_class: door

      - name: "East Front Window"
        state_topic: "433mhz/sensor_east_front_window"
        payload_on: "ON"
        payload_off: "OFF"
        device_class: window

You would also define you lights as entities. That means that you can read the state topic as well as command them and thus display the state of the light in the GUI regardless of how you turned it on.

For example:

mqtt:
  light:
    - name: "Garage Light"
      id: garage_front
      state_topic: "garagefront/cmnd/status" # whatever your device publishes
      command_topic: "garagefront/cmnd/power"
      qos: 0
      payload_on: "ON"
      payload_off: "OFF"
      # other config including state topics, brightness control etc
      optimistic: false

Your automation would then look like:

- id: GardenLightAllOff
  alias: Garden Light All Off
  trigger:
    - platform: state
      entity_id: binary.sensor.gardent_light_switch # whatever you defined
      from: "off"
      to: "on"
  action:
    - service: light.turn_on
      target:
        entity_id: garden_light_1, garden_light_2, garage_front, garage_weather
  mode: single

Even better - once the lights are entities you can assign them to a group and use the same service to switch the whole group on or off. If you add a light later you just need to change the group, the automation remains unchanged.

Hope this enough to get you started.

Yes, I can really work with this, thank you.
I’m a lot more comfortable with Python than any aspect of HA or YAML. You’d think YAML would be easy, but something very basic about it just escapes me.
On “efficiency” thought, we’re not doing any less processing: we’ve just passed the processing into a different part of the system. Without a doubt, it’s a LOT more readable and maintainable.