Check if Ikea tradfri lights are unavailable?

Hi is there a way to check if my light is unavailable, I need this because when I turn off my light switch it is not showing that it turned off in home assistant. the same happens when I turn the light switch back on. I first have switch the light on from home assistant to get it’s state updated. is there a way that home assistant checks the state every 2 sec, even when the device is powered off.

I’m not sure about what you need as a solution but I think I’m getting something similar. Sometimes, when I log in, my Tradfri lights are showing on, even though they’re off. Usually (I think) when they’ve been turned off or on using something else - like Alexa? Sometimes, when I toggle the switch, it just slides back on its own. A lot of the time, I have to restart HA to get everything working again. But this is only on my Tradfri lights. My Hive bulbs and plugs are always fine.

If you look at the services tab on the developer tools, you have:

homeassistant.update_entity

This is described as:

“Force one or more entities to update its data”

I don’t know if that could be run every few minutes with an automation. I’m only just getting into HA so I don’t know enough to try this. Might be worth a look though.

Thanks for the response,

I have tried this service before and it did not update the entity. I believe it’s only suppose to work when the device is online. because when I turn of my light with the switch on the wall and run this service under developer tools nothing happens. The state still has the status of the light when the switch was turned on.

Ah, ok. Have you looked at the States tab - < > - on the developer bit? Turn the switch off at the wall and then see what that returns. Personally, I’d then use Node Red to query the entity.

I’m assuming this is so you can see if someone has turned the switch off at the wall so you can turn it back on? I’m also assuming that the switch on the wall kills the power to the bulb rather than it being a smart switch?

I have tried to use this service in an automation but I got the same result. About my situation, I just use a normal light switch that kills the power of the bulb. I want to have it update the status because when I turn back on the light I have an automation that puts the brightness up to 100%. The automation does work but it takes about 30 sec for the systeem to see that the device is back online(because it checks for lights with an interval of 30 sec). I want to adjust that interval or achieve this with something else

IM using this for the exact reason. Only 1 light, because that gets its power from an external light sensor. During the day it has no power…

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

I customize the same light (well, the group) like this:

group.parking_lights:
  templates:
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';
    _stateDisplay: >
      if (entities['light.parking_light'].state === 'unavailable') return 'Power off';
      return '';

unfortunately support for _stateDisplay customization has been dismantled above 0.84.3 so I havent upgraded yet…

Please check the latest version of HA, because I think the Tradfri component has been updated to show unavailable if so.

3 Likes

Sorry for the late response, the Tradfri component does show up as unavailable but only when I switch the light on in home assistant when I have turned the wall switch off. I am not sure if it updates on it’s own, but if it does then I need a way to decrease the interval time. Also about the provided code I am not quite sure how to implement this in my home assistant. Could you explain the part that tries to update the light state?

sure, it turns the light on, with the exact setting is has when it is already on.(ie, when it is not unavailable).

if it cant turn it on, because the power is cut, (which it is during the day, when the daylight sensor switches off power) it shows unavailable.

this is a little trick, because the brightness setting is non existent when Off. using the |int makes the template work in that case, because it will then be 0.

just give it a try and see if it works for you, which I believe so.

Alright I will give it a try.
where do I need to put the second part of your code, does it also have to be in the automation file?

you mean the customization? It is to necessary at all, just for a bit of eye candy.

But it needs to go under homeassistant: customize:

depending on your configuration andyouo need to have custom-ui installed for it. But as said, not necessary for the automation to run and do its job.

homeassistant:
  customize:
    group.parking_lights:
      templates:
        icon_color: >
          if (state === 'on') return 'rgb(251, 210, 41)';
          return 'rgb(54, 95, 140)';
        _stateDisplay: >
          if (entities['light.parking_light'].state === 'unavailable') return 'Power off';
          return '';

I have tried to make it work but somehow I get an error

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

and here is the error:

Invalid config for [automation]: [minutes] is an invalid option for [automation]. Check: automation->trigger->0->minutes. (See /config/configuration.yaml, line 69). Please check the docs at https://home-assistant.io/components/automation/

Also is it possible to make it run every 2 sec? or is that a bit overkill

What Ha version do you use? Probably need the new time_pattern trigger, I don’t use yet because I’m still on Ha 0.84.3

See https://www.home-assistant.io/docs/automation/trigger/

I am using Ha 0.86.4, even when I try to use time_pattern it’s not working. I see what your code is doing so I have tried to make it work on this version, but unfortunately it doesn’t. when you are going to update to this version and get it to work please inform me. I will also try to make it work and will keep you updated.

What do you mean by ‘not working’? Doesn’t it trigger, doesn’t the condition evaluate to true, or doesn’t the action perform as expected…

When I switch of the switch the state of the light stays on, so it does not change to unavailable. and if i understand correctly the automation tries to turn the light on with the same brightness as the current brightness of the light, but I think it does not do that. When I manually try to turn it on it does say its unavailable.

Here is the code I used:

- id: '1549045493646'
  alias: Tradfri light shows unavailable
  trigger:
  - platform: time_pattern
    seconds: '5'
  condition: 
    condition: template
    value_template: >
        {{ states('light.slaapkamer') !='unavailable' }}
  action:
    service: light.turn_on
    data_template:
      entity_id: light.parking_light
      brightness: >
          {{state_attr('light.slaapkamer','brightness') |int }}

you might want to change the entity_id :wink:

also, every 5 seconds might be overkill? not even sure if the system responds that fast, so take it easy I would say.

lastly, you need the ‘/5’ to have it trigger every 5 seconds, (or minutes I would suggest)

Oh wow, I have been struggling with the time condition for so long that I messed up with the entity_id. :rofl: It’s now working, thank you for the amazing help.

1 Like

as always: you’re welcome, glad to be of help

Hi @Mariusthvdb ,
I know it’s an old thread but I don’t see any other post with this problem. Other than the GitHub issue, which is closed and I don’t know why. If you think I should post it in another place, tell me.
I was using your solution and it was working very well since a recent update. Since this update (don’t remember which one), set the brightness at the same level than it is now doing absolutely nothing. I tried to set via dev tools but nothing happens :frowning:
Have updated your solution?
I have try to rise the brightness +1 and then -1 but the solution is not perfect :frowning:

Any idea?

Thanks in advance for your help!!

Hi @jonasCr ,

I tried it this week and can confirm that it doesn’t work. I’m using HA 2021.3.4.

I then did some tests without the automation and came to this conclusion: HA understands that a bulb is not available only if, basically, a toggle event happens.

If the bulb was off when power was removed, HA must send an “on” command, and if it was on, HA must send an “off” command. If anything else, HA cannot figure out that the bulb is unavailable.

If that is indeed true, the automation of this thread cannot be used.