Koenkk / zigbee2mqtt

Zigbee šŸ to MQTT bridge šŸŒ‰, get rid of your proprietary Zigbee bridges šŸ”Ø
https://www.zigbee2mqtt.io
GNU General Public License v3.0
12.07k stars 1.68k forks source link

Lights turn on after restart of zigbee2mqtt - workaround? #992

Closed h4nc closed 5 years ago

h4nc commented 5 years ago

I have a xiaomi double switch that turns on my lights.

After a restart zigbee2mqqt publishes all the states again and so my automation in homeassistant gets triggered and turns on my lights.

Is there a way to bypass this? May turning of publishing the events at restart for certain devices?

My automation looks like this:

- alias: Zigbee Doppelschalter - left
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: sensor.zigbee_doppelschalter_click
  condition:
    - condition: state
      entity_id: sensor.zigbee_doppelschalter_click
      state: 'left'          
  action:
    - service: light.turn_on
      entity_id: light.licht

My config in zigbee looks like this:

  '0xXXX':
    friendly_name: Zigbee Doppelschalter
    retain: false
Koenkk commented 5 years ago

Probably a duplicate of #959

h4nc commented 5 years ago

I don't know if this really is the same issue. As my problem is pushing the states (probably saved in state.json) at the start of zigbee2mqtt causes this.

I also have the same issue with my ikea tradfri dimmer. If I turn of my light with alexa, the dimmer value will still be let's say "150". After a restart this will turn on the light.

This only would be a workarround but I could imagine two solutions:

Mind that this also could be a security issue. For instance if you want to trigger a garage door to open by a push button a restart of zigbee (caused by a power failure) would open the garage door.

EDIT: another idea: maybe it is possible to add an information to the pushed mqtt topic. We could use this information for a condition in ha. This information should contain if zigbee just started or it is a real trigger.

Koenkk commented 5 years ago

Stop when home assistant is not running, do you also experience this?

h4nc commented 5 years ago

I don't understand. When home assistant does not run, there is no automation to be triggered, so in this case no lights will turn on. Or do you ask if the "start messaged" appear in zigbee2mqtt

Koenkk commented 5 years ago

When using the automation format from the wiki, does this also happen (https://koenkk.github.io/zigbee2mqtt/integration/home_assistant.html - Responding to button clicks)

h4nc commented 5 years ago

Looks like this works. The reason is that those three values are reportet at the start up:

{"battery":100,"voltage":3035,"linkquality":42}

click is not included, so the automation is not triggered.

So restarting zigbee doesn't trigger that anymore.

To be totaly sure I still have to test if a ha or a mosquitto restart will trigger this. Maybe at the start up of mosquitto it fired those messages again. I don't know that. Do you?

EDIT: I just found out the following: So responding to button clicks will work for the aqara buttons (click is not reportet in the mqtt message at start up of zigbee2mqtt). The ikea dimmer does report its brightness value at startup. So this one will be triggered.

{"brightness":233,"linkquality":15,"last_seen":"2019-01-20T14:33:06.418Z","battery":87}

This might be an issue for other devices too. It depends on wch data they report at startup. I guess the coord pings those devices at startup??? and than gets those values, right? The device "decides" which information it sends.

Koenkk commented 5 years ago

No that shouldn't be the case.

Let me know if this can be closed.

h4nc commented 5 years ago

I edited my comment above. This will still be an issue for devices like the ikea dimmer.

Koenkk commented 5 years ago

The brightness of the Tradfri dimmer is something added by zigbee2mqtt. Stateless attributes like ('click', 'action') shouldn't be reported, but stateful (like brightness) should be.

What is the problem that this will be triggered?

h4nc commented 5 years ago

Because this trigger will turn on lights with the reported brightness value.

If you turn off a light (not with the dimmer) and than restart zigbee2mqtt the brightness value will trun on that light again.

Edit: Also I jsut found out that a restart of home assistant triggers that automation? do I have to change something at the retain setting?

this automation is triggered at start up, don't know why. There is another mqtt message after the restart, but it doesn't contain "click", so I can't explain my self why this gets triggers. And even more weird, the "right" side doesn't get triggered (even if I press the right button before, so it has that state saved).

- alias: Zigbee doubleswitch - left
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee doubleswitch'
  condition:
    condition: template
    value_template: '{{ "left" == trigger.payload_json.click }}'        
  action:
    - service: light.toggle
      entity_id: light.licht
    - service: notify.ios_iphone
      data:
        message: "left triggered"  
Koenkk commented 5 years ago

@h4nc I see, can you try adding brightness to https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/extension/deviceReceive.js#L4?

h4nc commented 5 years ago

So there is no issue with the dimmer, if you only use the dimmer to control a light.

If you use other ways too: voice control, ha UI than the state of the dimmer will cause issues.

However could you explain a little more what I should do in that code?

Edit: I donā€™t think I can change that code as I donā€™t know how to this in the hassio addon. But if you change that I can pull the update and try it out.

Also I tried restarting home assistant and mosquito. The left button gets triggered. I think it has something to do with the retain state. But it looks like the left one is the to retain true and the right one to false (as this one doesnā€™t get triggered). In the configuration I have it set to retain: false.

h4nc commented 5 years ago

I donā€™t know why but it looks like the retain flag was set to true, so mosquito published the information at the restart. Do you have an explanation why the retain flag went to true? Just asking, because Iā€™d like to know if this could happen again.

To get rid of this I deleted my mosquito addon and reinstalled it. Thus probably isnā€™t the smartest way to do that. But I knew this would delete the database. Is there another way to do this in future?

Koenkk commented 5 years ago

@h4nc retain flag is set manually in configuration.yaml. A retained MQTT messages can be cleared by sending a message to the same topic without the retain flag.

h4nc commented 5 years ago

Thanks for clearing that. To still it will be easier to delete and reinstall to clear the database, as finding the right topic and publish.

I still donā€™t get why my left button was retain: true.

Iā€˜m thinking about using one of those Aqara sensors to trigger a security sensitive automation. But before I do this I must be a 100% sure the automation is only triggered when I push the button. And not from a restart of similar things.

Koenkk commented 5 years ago

@h4nc agree! We are trying find the correct automation in #959. For now, the MQTT trigger can be used.