Another automation question

Hi Guys,

I’ve just another automation question but first let me explain what I’m trying to achieve.

I’ve ordered a few ESP8266 and I want to create a switch for some lights e.g. I’m using the limitless led.

Now an issue I have is I need to match the status of essentially 2 switches as an example the limitless led has no clue about the mutt switch so I created the following automation to turn on and off the lights if e.g. the button is pressed on my ESP8266 switch and also two more automations to match the state to the ESP8266 if the light is switched on via the WebUI.

I came up with this:

###  Turn on light when MQTT button is pressed
- alias: MQTT button livingroom on
  hide_entity: true
  trigger:
      - platform: state
        entity_id: switch.mqtt_test
        to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: light.livingroom_door
      
###  Turn off light when MQTT button is pressed
- alias: MQTT button livingroom off
  hide_entity: true
  trigger:
      - platform: state
        entity_id: switch.mqtt_test
        to: 'off'
  action:
    - service: homeassistant.turn_off
      entity_id: light.livingroom_door

## Update light when light is turned off by other means
- alias: MQTT state button livingroom on
  hide_entity: true
  trigger:
      - platform: state
        entity_id: light.livingroom_door
        to: 'on'
  action:
    service: mqtt.publish
    data:
      topic: 'home/test/stat'
      payload: 'on'

      
###  Update light when light is turned off by other means
- alias: MQTT button livingroom off
  hide_entity: true
  trigger:
      - platform: state
        entity_id: light.livingroom_door
        to: 'off'
  action:
    service: mqtt.publish
    data:
      topic: 'home/test/stat'
      payload: 'off'

Now I’m wondering is there any better way to do this ? This would be 4 automation rule per switch I’d like to reduce this but unsure how to do it. I will need to match the status because if I switch on the light via the webui then the switch will think it’s still off and on the first press try to turn it on which isn’t the correct thing to do that means I need to somehow sync the states.

Any ideas suggestions etc appreciated.

You can have multiple triggers for an automation. So try something like this:

  trigger:
      - platform: state
        entity_id: switch.mqtt_test
        to: 'on'
      - platform: state
        entity_id: light.livingroom_door
        to: 'on'

This would trigger if the mqtt switch OR the light.livingroom_door turned on.

Thanks for the idea but if I’m not mistaken that would not update the state depending on how I’ve triggered the action or can I somehow still say which action to trigger if one of the event’s occurs ?

You can use more than one action, too. Will that work for you?

action:
    - service: homeassistant.turn_on
      entity_id: light.livingroom_door
    - service: mqtt.publish
      data:
        topic: 'home/test/stat'
        payload: 'on

I’d say it does the trick and reduces the automations by half !

Thanks a lot

When you start getting into more advanced stuff, you can also make use of the trigger values in your conditions. So you can test which trigger was true or refer to payloads, etc.

That’s looks pretty good I’ll have a look how I can use it in my scenario that said Home-assistant really has a ton of options to go through.