Get event origin

I need to know what’s the origin of the event in self.listen_state(xx)

2 use-cases:

  1. I have some 4 gang switches in a room and some relais boards in the distrubution box (which controls the lights). Some of the switches trigger the same relais (light).
    I create a script that listen the states of the switches and lights and when some of then changed it’s state, the other switches get the same state. (like a cross switch).
    Most of the time, the script works but because of the slowlyness of the wifi, when you push fast at one switch, the lights start flickering (probably because of timing problems).
    I can prevent this by only listen to state-changes caused by the (manual) switches itself and not by the automation-triggers.

Simplyfied code:

    def initialize(self):
        for ent in self.args["entities"]:
            self.listen_state(self.switchClick, ent)

    def switchClick(self, entity, attribute, old, new, cb_args):
        for slaveEnt in self.args["entities"]:
            if slaveEnt != entity:
                self.changeState(slaveEnt, old, new)
                
    def changeState(self, entity, old, new):
        if new == "on":
            self.turn_on(entity)
        else:
            self.turn_off(entity)
  1. Other use-case is getting the event-source of my climate entity. I have a script that force the airconditioning to go on/off but only when you manually set it on. So when I know what’s triggers the on/off, I can solve this problem on a proper way.

Thanks in advance!

1 Like