Tasmota sonoff and shelly toggle between off and on

Hi Everybody.

I have a problem with my tasmotized switches. I’m controlling my toilet light using a sonoff basic, flashed with tasmota. The light turns on after an automation, triggered by an binary 443mhz door sensor (2 way). The lights should turn back off again, using another automation, which has a condition to check if the light is on. This automation is triggered again by the binary sensor.
Now the problem is: when the light is on, and you leave the toilet, the light quickly flashes off and back on. It’s not possible to turn off the light. If you toggle the binary sensor again, by opening the door, the same thing happens.
I recorded 2 video’s, so that it’s easier to see what happens.
Youtube link 1 (sonoff basic)
Youtube link 2 (shelly)
This problem is with multiple devices. Shelly1, sonoff basic, Sonof dual R2.

The automation for the switches:

alias: Toilet door open, light on.
trigger:
  - entity_id: binary_sensor.toilet_door
    platform: state
    to: 'on'
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.toilet_light

alias: toilet door open and light on, then turn light off.
trigger:
  - entity_id: binary_sensor.toilet_door
    platform: state
    to: 'on'
condition:
  - condition: state
    entity_id: switch.toilet_light
    state: 'on'
action:
  - service: switch.turn_off
    entity_id: switch.toilet_light

I can’t figure out where this problem comes from. Reflashing the sonoffs doesn’t seem to help.

my guess would that since you have the same trigger in both and the action does the exact opposite )one turns on the light, the other turns it off) there is likely some interference between the two based on timing.

try adding a 1 or 2 second delay in the action section in both automations to see if that fixes it.

I had this exact problem with one of my automations and this fixed it as well.

Yes, that seemed to did the trick! Although if i did added it in the action in both automation, i had the same problem. Now I only have it in the light off automation, and that works. Thanks!