home-assistant-libs / pytradfri

IKEA Trådfri/Tradfri API. Control and observe your lights from Python. Examples available. On pypi. Sans-io.
MIT License
936 stars 131 forks source link

Test if Tradfri light is available/reachable? #196

Closed Mariusthvdb closed 5 years ago

Mariusthvdb commented 5 years ago

Where are you using pytradfri (eg stand-alone, Home Assistant etc)

Homeassistant

Version of pytradfri

Expected behaviour

Please consider adding functionality of checking reachability of Tradfri-lights.

Actual behaviour

Right now, when power is cut off to the lights, they still show on or off. In the app the lights show unavailable (grey out) so some sort of check is already implemented in the Tradfri system.

please see community-post: https://community.home-assistant.io/t/test-if-tradfri-light-is-available-reachable/74314

if reachability would be implemented we can do this in the Ha frontend, and, create automations and other template sensors based on that reachabilty:

schermafbeelding 2018-10-21 om 14 11 32

schermafbeelding 2018-10-21 om 14 16 58 schermafbeelding 2018-10-21 om 14 17 10

Driveway is a Philips Hue light tested with:

- platform: command_line
  name: Driveway reachable
  command: !secret driveway_command
  value_template: >
    {{value_json.state.reachable}}

Parking is a Tradfri light showing to be on, while in fact it is unavailable while power is cut.

ggravlingen commented 5 years ago

I did some investigation here and in the code base of Home Assistant and it seems (which I had forgotten) the feature is already available. In pytradfri, the reachable state is exposed here: https://github.com/ggravlingen/pytradfri/blob/3e3d68fb5b14c497b45548085778261fbd3c0ff7/pytradfri/device.py#L60

And in Home Assisstant, here: https://github.com/home-assistant/home-assistant/blob/7f0a50ce31befab10e8ba1892cc30b1334f22fc2/homeassistant/components/light/tradfri.py#L359

I also tried switching of my light using the wall switch and the light does show up as unavailable: image

So, in conclusion. It already does work :)

Mariusthvdb commented 5 years ago

Not sure how you did this , but it is exactly what I am hoping for . If power is cut off here , the lights show with their last state, I haven’t seen unavailable on any of the TRÅDFRI lights yet.

Is the feature enabled in the current release software ??

ggravlingen commented 5 years ago

Just to be sure: you are running the tradfri gateway and not a hue?

Mariusthvdb commented 5 years ago

Yes I am. Both in fact ;-) But only I use the dedicated setup: TRÅDFRI lights on the TRÅDFRI hub and hue lights on the hue hub.

ggravlingen commented 5 years ago

I'm running v80.3 of Home Assistant, which runs the latest version of pytradfri. But there have not been any changes to this part of the code for long. What happens if you turn of a light using the switch and then try to adjust, for instance, brightness?

Mariusthvdb commented 5 years ago

a yes, now i see that too: unavailable. Never noticed before. Thats a start! It needs a command/change of setting though and doesn't change to unavailable automatically when power is cut.

enabling power does change to the correct state automatically.

So we'd need only to adjust for the situation of power cut off.

ggravlingen commented 5 years ago

That's something that needs to be fixed in home assistant so we should close the issue here.

(I'm not enough versed in the inner workings of home assistant to provide a fix in home assistant I'm afraid.)

Mariusthvdb commented 5 years ago

ok, where should we go from here then? the regular homeassistant GitHub issue tracker?

I have filed this issue: https://github.com/home-assistant/home-assistant/issues/17675

ggravlingen commented 5 years ago

Yes and hope that someone picks the issue up. As a temporary workaround, perhaps you could trigger an automation that sends the last observed brightness level every five minutes or so? Just to trigger the unavailable state.

Mariusthvdb commented 5 years ago

Yes and hope that someone picks the issue up. As a temporary workaround, perhaps you could trigger an automation that sends the last observed brightness level every five minutes or so? Just to trigger the unavailable state.

yes, thanks, I've been thinking about the best workaround for now, a bit like the reachable command_line sensor for the Hue lights. Not sure if a regular brightness check does it, or we would need an actual state change. Will test several ways, and report back if helpful. Please don't hesitate to do so too ;-) Thanks!

--edit-- simply creating a value_template {{states(‘light.parking_light’)}} or even {{state_attr(‘light.outside’,‘parking_light’)}} doesn’t work, it keeps showing the last state or brightness setting. I guess we need to send a command for it to show unavailable

also, if power is cut at (before) startup the unavailable does show correctly.

lets see what testing this will bring:

  - alias: "Dummy set Tradfri light to show unavailable"
    id: "Dummy set Tradfri light to show unavailable"
#    initial_state: 'on'
    trigger:
      platform: time
      minutes: '/5'
      seconds: 00
    condition: 
      condition: template
      value_template: >
        {{ states('light.outside') != 'unavailable'}} # no need to test if already showing unavailable
    action:
      service: light.turn_on
      data_template:
        entity_id: light.outside
        brightness: >
          {{ state_attr('light.outside','brightness') | int }} # set same brightness the light is actually set to, not to have it changing all the time....

doing fine (needs the |int)

ggravlingen commented 5 years ago

This issue does not relate to pytradfri, but rather the Home Assistant project. Closing the issue so that the discussion can be continued in their repo.