Solution doesn’t work if bulb was cut off from the electricity in ON state. So it keeps ON state all the time.
But idea is great, I will try to do something based on it.
If you noticed, the first command is to turn off the bulb. It’s exactly for that reason. If it was cut in ‘on’ state and turned on later, then it will first try to turn_off, then turn_on and then check if it’s online
Hmmm, just noticed that after the last update of zigbee2mqtt, if the bulb loses power and regains it, hass is informed.
I have probably same setup as you. I have a cc2531 as a coordinator, zigbee2mqtt and an ikea e27(i think) bulb(the bulb surely acts as a router too). With previous versions I had the same problem but didn’t mind as I have an IKEA button too so I did it manually.
I literally noticed 1 minute ago that currently if I cut power in off state and restore power(in which the bulb starts at ‘on’ state), hass entity is also turned on! Maybe check that out? If that’s the case, then the configuration I posted earlier is spot on!
your configuration works in general
I made some changes and it can detect the state as I want
now the problem is that it needs to blink to detect the state
trying to figure it out
Ok, did some more digging with my setup. So, in reality, when the lamp is cut off from mains in EITHER state, hassio can’t know it. BUT. If it’s cut in off state and it’s turned on, it also informs hass of that and the switch is turned on. If it’s cut in on state and returns, the value stays the same. But I’ve noticed something else that the Hass does. In EITHER state it was cut from mains, it can’t switch state as long as it’s offline!
So, a new type of “dirty”(again!) automation would be:
- service: variable.set_variable
data_template:
variable: ikeacheck
value: >
{% if is_state('light.0x000b57fffe9151b8_light', 'off')
0
{% else %}
1
{% endif %}
- entity_id: light.0x000b57fffe9151b8_light
service_template: >
{% if is_state('light.0x000b57fffe9151b8_light', 'off')
light.turn_on
{% else %}
light.turn_off
{% endif %}
data:
brightness: 1
- delay:
seconds: 2
- service: notify.android
data_template:
message: >
{% if is_state('light.0x000b57fffe9151b8_light', 'off' ) and is_state('variable.ikeacheck', '1') %}
Bulb is online
{% elif is_state('light.0x000b57fffe9151b8_light', 'on' ) and is_state('variable.ikeacheck', '0') %}
Bulb is online
{% else%}
Bulb is offline
{% endif %}
- entity_id: light.0x000b57fffe9151b8_light
service: light.turn_off
I may have some typos, kindly check the code if it’s ok. I’ve added a variable too, so if you don’t already have it, install from https://github.com/rogro82/hass-variables. It will come in handy in many occasions you’ll need to store a value (either for tts, for checking entities against previous states etc).
So, what it does is:
- Check the current state of the lamp and store it on a variable
- Turn on or off, depending on state. Note that I added a brightness value to 1 so it will come on in minimum brightness so it won’t bother much. BUT when the light is already on and tries to turn off, it will show an error in logs! Be aware of that. It will still continue to run the automation, just reports an error because it cannot set brighness when turning off.
- Wait 2 secs
- Check the current state against the previous state stored in the variable. If it changed state, it means the lamp is working. If not, it’s offline or out of reach.
And last, if the bulb was off and we turned it on with the automation, turn it off again.
Hope it helps. Just seems a LOT for checking just a light. Maybe a better solution is to replace your current lamp switch (button? I can’t recall how it’s named!) with a wifi one. That way, anytime someone presses the switch, making the lamp offline, you can have your automations check the state of the switch and act accordingly. It will be a way more clean way that trying to turn on and off the lamp.
Thank you very much for your scripts.
I came almost to the same solution but with binary sensor for storing initial state.
It works perfectly for switched OFF bulb.
But if it is switched ON it will blink for around 3-4 seconds. (delay * 2)
Unfortunately hassio doesn’t return back brightness to the previous state if bulb is offline.
So you can not instead of switching ON and OFF change brightness to 1 step.
My final script is
- alias: Test bath bulb if on
trigger:
platform: time_pattern
minutes: '/1'
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.bathroom_motion
state: 'off'
- condition: state
entity_id: light.bathroom_light
state: 'on'
action:
- entity_id: light.bathroom_light
service: light.turn_off
- delay:
seconds: 2
- service_template: >
{% if is_state('light.bathroom_light', 'on') %}input_boolean.turn_off{% else %}input_boolean.turn_on{% endif %}
entity_id: input_boolean.bath_bulb
- entity_id: light.bathroom_light
service: light.turn_on
If bulb has state ON, does’t matter cutoff or online, it checks every minute if it is online. But I don’t want the bulb blinking when someone is in the bathroom. So I check only if my motion sensor is in “off” state. I don’t care much about the delay so this automation works for me.
Special and huge thanks to @pvakerlis !!
Nice!
I would change
- service_template: >
{% if is_state('light.bathroom_light', 'on') %}input_boolean.turn_off{% else %}input_boolean.turn_on{% endif %}
to
- service_template: >
{% if is_state('light.bathroom_light', 'on') %}
input_boolean.turn_off
{% else %}
input_boolean.turn_on
{% endif %}
Just to look clean, no other reason. If you’re fine with it, doesn’t matter anyway!
Did you check it with power cuts etc? Is it working properly?
Also, why do you turn on the light in the last line as long as noone is in the bathroom?
Works properly in all cases.
My light and ventilation has weird connections. It is really hard to change that, don’t ask why. So fixing hardware issues with the software.
All that code is to be able to use ventilation not only when the light is on, but also when humidity is high.
Haha tell me about it! I got a similar issue but not with hardware, but a girlfriend! In the end I figured it’s easier to write extra 10 automations and train Home Assistant to react accordingly than train my girl! xD
Glad I could help!
Hi guys,
I had the exactly same problem as you and I think I have found a solution… It is built into the zigbee2mqtt configuration but not available from the hassio add on config… Doesn’t require any scripts.
See https://community.home-assistant.io/t/zigbee2mqtt-light-still-showing-up-even-when-powered-off/140167
or, if you like clean templates:
- service_template: >
input_boolean.turn_{{'off' if is_state('light.bathroom_light', 'on') else 'on'}}
I actually got this to work without any automation. Just zigbee2mqtt configuration and correct implementation of the MQTT sensor in Home Assistant. The important part of the zigbee2mqtt configuration which I needed was as follows:
availability_timeout: 60
The above will make zigbee2mqtt ping your battery powered devices (IKEA light bulbs) every minute. Change it to something else if this is too often. More information about that here: https://www.zigbee2mqtt.io/information/availability.html
In the Home Assistant configuration I had to add the following to my MQTT sensor:
availability_topic: 'zigbee2mqtt/<your_device>/availability'
For my kitchen light, I use the following:
platform: mqtt
name: kitchen
state_topic: 'zigbee2mqtt/light_kitchen'
command_topic: 'zigbee2mqtt/light_kitchen/set'
availability_topic: 'zigbee2mqtt/light_kitchen/availability'
json_attributes_topic: 'zigbee2mqtt/light_kitchen'
brightness: true
schema: json
Turning the light on with the normal wall switch and having it on (virtually) in Home Assistant and than turning it off using the wall switch will… in Home Assistant it will still be on, but after (at most) 60 seconds when the light is pinged, it will turn into ‘unavailable’ in Home Assistant.