Detect online/offline state of ikea zigbee bulb

Hello Community,
I need to detect if light is switch ON or OFF in one of my rooms.
I have a bulb from Ikea and it is connected to hassio via zigbee2mqtt. It works just like a regular bulb with the regular switch (together with other bulbs in that room). I want to detect if my (regular)switch is in OFF or ON state.
As I understand bulb should be offline if switch is OFF and online if switch is ON But I couldn’t find a way how to detect this state of the bulb.
Is there some solution for that?

Nothing?
No one knows or it is just a stupid question?
Maybe other ways to detect if light is switch ON/OFF with minimum setup?

Using deCONZ the light would be marked as unavailable in HASS. Can’t say for Z2M

With Tradfri gateway the state is also ‘unavailable’ when the physicial switch is turned off. I also don’t know about Zigbee2MQTT.

I think the template to detect what you are asking for would be:

{{ not is_state('light.leeslampen_woonkamer','unavailable') }}

You can use that template e.g. in a template sensor:

sensor:
  - platform: template
    sensors:
      wall_switch:
        friendly_name: "Wall switch"
        value_template: "{{ not is_state('light.leeslampen_woonkamer','unavailable') }}"

Unfortunately it doesn’t report state “unavailable”. Looks like it keeps the last state when it was online.

Perhaps this is of use? Test if Tradfri light is available/reachable?

yes, this works just fine. idea: turning on a light, that is not powered forces the state ‘unavailable’ to show.

let me know if it works out for you

It doesn’t work because if it was cut off in ON state. You can not switch it off. It still reports as ON.
Other way around as well.

the automation doesn’t switch off, it switches on. If, doing so, the light is unavailable , which it is when cut off, it should show unavailable…
thats the whole purpose of the automation.

just to be sure, this is the automation working just fine:

  - alias: 'Tradfri parking light shows unavailable'
    id: 'Tradfri parking light shows unavailable'
#    initial_state: 'on'
    trigger:
      platform: time_pattern
      minutes: '/5'
      seconds: 00
    condition: 
      - condition: template
        value_template: >
          {{ states('light.parking_light') !='unavailable' }}
      - condition: template
        value_template: >
          {{ is_state('binary_sensor.outside_daylight_sensor','on')}}
    action:
      service: light.turn_on
      data_template:
        entity_id: light.parking_light
        brightness: >
          {{state_attr('light.parking_light','brightness') |int }}
1 Like

This automation doesn’t do what you describe for me.
I see in zigbee2mqtt logs that it tries to change the state. But lamp doesn’t get “unavailable” state in hassio.

Well, what can I see, it does in my setup

This light is cutoff power daily and set to unavailable using this automation.

That’s super strange. Which version of zigbee2mqtt you are using?

I don’t. Use the IKEA Hub

C’mon! The thread is about zigbee2mqtt integration. Sometimes its better to read before commenting.

please behave.
in the first place was I tagged, and responded to that.
secondly this thread is not about zigbee2mqtt, there’s another thread about that indeed. not here. title is self explanatory.
thirdly, I was trying to help.

hope you will find your answer.

2 Likes

@santik Did you ever get this solved? I have the exact same issue. I have several IKEA Tradfri bulbs and used them with the Gateway. I got rid of the Gateway by moving to zigbee2mqtt and indeed the only states I get for a light now are either on or off.

I used to get unavailable as a state when using the Gateway instead of zigbee2mqtt. I set up my IKEA Tradfri bulbs manually in YAML using the configuration at the bottom of e.g. the following page: https://www.zigbee2mqtt.io/devices/LED1545G12.html.

In my zigbee2mqtt configuration.yaml I have all of my devices (in this specific case we’re talking about light bulbs) use the retain attribute set to false. Thinking it wouldn’t keep the on state when the regular in-wall switch is turned off, but change to unavailable. It didn’t.

If you haven’t solved this yet, I’m going to check out the zigbee2mqtt Github issues page (which I haven’t done yet), since it seems to be an issue with that library. Perhaps someone has had the same issue there. If not, I’ll create an issue.

Unfortunately no. I tried many things including custom scripts but nothing helped. Would be nice if they can update library.

A dirty way to figure if it’s unavailable, is to try and switch it on. Zigbee2mqtt by default doesn’t mark a bulb unavailable, it just keeps it’s last state. But if the bulb is actually unavailable (or out of reach) it will try to turn it on and after 1 second it will show turned off again. So you could use an automation like
Turn on the lamp
Wait 1 second
Check if lamp is turned on. If it’s still on, turn it off. If not, mark it unavailable.

I tried that but failed. Do you have working example of this automation?

Here’s an automation that will notify you when the bulb is unavailable.

- alias: Test bulb at night
  trigger:
    - platform: time
      at: "04:00:00" 
  action:
    - entity_id: light.0x000b57fffe9151b8_light
      service: light.turn_off
    - delay:
      milliseconds: 400
    - entity_id: light.0x000b57fffe9151b8_light
      service: light.turn_on
    - delay:
      seconds: 2
    - service: notify.android
      data_template: 
        message: >
          {% if is_state('light.0x000b57fffe9151b8_light', 'off')
          Bulb is offline
          {% else %}
          Bulb is online
          {% endif %} 
    - entity_id: light.0x000b57fffe9151b8_light
      service: light.turn_off       

This is just to notify you on your phone if the bulb is online or offline.
Keep in mind that with this automation and zigbee2mqtt, unavailable will also mean out of reach! So either if the bulb is off by the switch, or the gateway can’t reach the bulb due to wireless coverage, the result will be the same.
The only thing that zigbee can detect is the response from the bulb when it’s turned on. That means that if the last state of the bulb was on, you can turn it off from home assistant with no problems as it doesn’t wait for a response. But when you turn it on, the gateway will either get a response and mark it on, or it won’t and switch back to off. You can test this in lovelace if you have the switch. When the bulb is online and you turn the lovelace switch on, the light next to it will also light up. But if it’s offline or out of reach, the switch will turn on for 0,5 to 1 second, the light next to it will never light up and it will return to off by itself.
Also, you cannot set the state of the device to unavailable, but you can set a binary sensor to do that. Seems like an overkill to me.
If you don’t use brightness and just use it as a regular bulb (meaning only on/off), just install a sonoff wifi switch. It will do the same job and keep states properly.

1 Like