Automation works on WebUI but does not work with the switch

Tags: #<Tag:0x00007f325fd106f8> #<Tag:0x00007f325fd10450> #<Tag:0x00007f325fd102e8>

Hello, thank you in advance for reading my question.

I have an automation script as follows:-

alias: Kitchen - Master Switch Off
description: ''
trigger:
  - platform: state
    entity_id: sensor.tradfri_on_off_switch
condition: []
action:
  - entity_id: light.kitchen_led_plug
    service: light.turn_off
  - entity_id: light.tradfri_bulb_4
    service: light.turn_off
  - entity_id: light.tradfri_bulb_5
    service: light.turn_off
  - entity_id: switch.sonoff_100033c557
    service: switch.turn_off
  - entity_id: switch.sonoff_100033c860
    service: switch.turn_off

Clicking the Execute button in the Automations YAML editor successfully turns off the 5 entities (and vice versa. I have another identical automation script except it says on)

My issue is that physically pressing the IKEA on/off switch in the kitchen does not turn on/off the related entities. It only turns off the IKEA bulbs and ignores the plug and Sonoff.

Being a noob, I’ve searched and tried different syntax but to no avail. Any piece of advice will be greatly appreciated.

Cheers,

T

Issue could be you are not mentioning the state change in these automations. You already said you have two identical automations. Both these have no trigger state change mentioned so they both might be executed simultaneously. Try with state changes like this

alias: Kitchen - Master Switch Off
description: ''
trigger:
  - platform: state
    entity_id: sensor.tradfri_on_off_switch
    from: 'on'
    to: 'off'
condition: []
action:
  - entity_id: light.kitchen_led_plug
    service: light.turn_off
  - entity_id: light.tradfri_bulb_4
    service: light.turn_off
  - entity_id: light.tradfri_bulb_5
    service: light.turn_off
  - entity_id: switch.sonoff_100033c557
    service: switch.turn_off
  - entity_id: switch.sonoff_100033c860
    service: switch.turn_off

In the second identical automation change the trigger state from off to on.

This single automation turns the lights and switches on or off depending on the sensor’s state.

alias: Kitchen - Master Switch On or Off
description: ''
trigger:
  - platform: state
    entity_id: sensor.tradfri_on_off_switch
condition: []
action:
  - service: 'homeassistant.turn_{{ trigger.to_state.state }}'
    entity_id:
      - light.kitchen_led_plug
      - light.tradfri_bulb_4
      - light.tradfri_bulb_5
      - switch.sonoff_100033c557
      - switch.sonoff_100033c860

NOTE:

This automation will work only if the sensor reports its states as on or off. If it doesn’t then the automation will not work correctly.

You mentioned that you observed different behavior when you executed the automation manually compared to when the automation was triggered by the sensor. In the first case, all 5 entities were turned off, in the second case only the lights were turned off and not the two Sonoff switches.

Manually executing the automation skips its trigger and simply executes its action. That means the action does work but there is something strange happening with the trigger because when the sensor changes state only the lights are controlled (not the switches).

1 Like

Thanks for the quick reply. Unfortunately, that did not resolve the problem. :frowning:

I will try this now.

Did you change the second automation with the reversed state change condition?

Unfortunately, this did not work at all. Do I need to restart the server?

Yes, as a matter of fact, both automation scripts did have the right state specified but did not work. As I have mentioned previously, the issue is that it works on WebUI - no problem when Execute button is pressed. Only when I press the physical switch, it ignores some of the entities, namely the none-Ikea entities.

Minimally, you need to execute Configuration > Server Controls > Reload Automations. However, as mentioned, it won’t work if the sensor doesn’t report its states as on or off.

Go to Developer Tools > States, find the sensor, and look at what it reports as its state. Is it either on or off?

Ok. I will try this now.

I thought it could have been the issue as the state change in trigger condition was not seen in the shared automation.yaml

Huh. This is what I see.

That’s important information. Clearly the sensor’s state is neither on or off (so the automation I posted will not work). In fact, this appears to report the sensor’s battery level and is not an appropriate sensor to trigger your automation.

Effectively, your original two automations will only trigger when the sensor’s battery level changes.

Earlier you wrote (but then removed) that the device is still linked to the three IKEA lights. That’s why it continues to control the lights but not the Sonoff switches (because the automation isn’t automatically triggering at all).

Gotcha. The WebUI will literally run the script from the top ignoring any defects, so long as there is no syntax error, hence why. Let me try this with a Xiaomi switch!

Manually executing an automation ignores the trigger and the condition (if it exists). In this case, the trigger is based on battery level so it will only trigger when the sensor’s battery level changes and not when you operate the remote-control. Basically, you’ve chosen the wrong entity to trigger your two automations.

Go to Configuration > Devices and find the device you are using to control the lights (and let us know what it is). It will list all the entities that represent this device. One of those entities will be the battery level sensor but you want an entity that reports the device’s state.

1 Like

Having this issue as well. My automation used to work flawlessly but has slowly been breaking down. Worked perfectly a few months ago, then last week it was intermittent at best, and now it only works from my Lovelace on/off switch while neither actual wall switch triggers no automation at all.

My Lovelace switch was just the Master Bedroom Lights, but because of the automation it would always trigger the hall light, too. It still does, but now it’s the switches on the wall that don’t trigger the opposite switch at all.

What am I missing?

################################################################################
- id: hall_mirror_master
  alias: Master Hall Light = Master Bedroom Light
  trigger:
    - platform: state
      entity_id: light.master_hall_light
  action:
  - data_template:
      brightness: "{{ state_attr('light.master_hall_light', 'brightness') | int }}"
    entity_id: light.master_bedroom_lights
    service: light.turn_on
################################################################################
- id: master_mirror_hall
  alias: Master Bedrom Light = Master Hall Light
  trigger:
    - platform: state
      entity_id: light.master_bedroom_lights
  action:
  - data_template:
      brightness: "{{ state_attr('light.master_bedroom_lights', 'brightness') | int }}"
    entity_id: light.master_hall_light
    service: light.turn_on