Keeping state of Philips Hues in Appdaemon

Hues state in Appdaemon.

Since I am using Appdaemon, it was obvious to rewrite my previous script to run in Appdaemon. The description of how it is working is also found in that blog entry.

Here is the configuration that needs to be put in conf/appdaemon.yaml :

HueStateKeeper:
  module: huestatekeeper
  class: HueStateKeeper
  bridge: 192.168.xxx.xxx
  lights: Wall,Table,Floor

The names of the lights are the names defined in the Hue Bridge.

And the file conf/apps/huestatekeeper.py looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import appdaemon.appapi as appapi
from phue import Bridge
import datetime

class HueStateKeeper(appapi.AppDaemon):
    db_brightness={}
    db_xy={}
    db_on={}
    db_restoring={}

    def initialize(self):
        self.log("Initializing Philips Hue State Keeper.")
        self.bridge = Bridge(self.args["bridge"])
        lights = self.bridge.lights
        self.watchedlights = self.split_device_list(self.args["lights"])

        for light in lights:
            name = light.name
            if name in self.watchedlights:
                self.log("Now spying on light: {}".format(name))
                self.db_brightness[name] = 0
                self.db_xy[name] = 'nonexist'
                self.db_on[name] = False
                self.db_restoring[name] = False
        self.timerhandle = self.run_every(self.check_hue_states,datetime.datetime.now(),1)

    def check_hue_states(self, kwargs):
        lights = self.bridge.lights
        for light in lights:
            name = light.name
            if name in self.watchedlights:

                if light.reachable:
                    if self.db_restoring[name]:
                        self.log ("{} is online again and should be restored to previous state.".format(name))
                        self.db_restoring[name] = False
                    else:
                        if light.on != self.db_on[name]:
                            self.log ("{} saved new state: {}.".format(name, "On" if light.on else "Off"))
                            self.db_on[name] = light.on
                        if light.brightness != self.db_brightness[name]:
                            self.db_brightness[name] = light.brightness
                            self.log ("{} saved new brightness: {}.".format(name, light.brightness))
                        if hasattr(light,"xy") and light.xy != self.db_xy[name]:
                            self.db_xy[name] = light.xy
                            self.log ("{} saved new xy: {}.".format(name, light.xy))
                else:
                    if self.db_on[name]:
                        if not self.db_restoring[name]:
                            if self.db_xy[name] != "nonexist": self.log ("{} is offline. Restoring brightness: {}.".format(name, self.db_brightness[name]))
                            self.log ("{} is offline. Restoring xy: {}.".format(name, self.db_xy[name]))
                            self.db_restoring[name] = True
                        if self.db_restoring[name]:
                            light.brightness = self.db_brightness[name]
                            if self.db_xy[name] != "nonexist": light.xy = self.db_xy[name]
                    else:
                        if not self.db_restoring[name]:
                            self.log ("{} is offline. Restoring state: Off.".format(name))
                            self.db_restoring[name] = True
                        else:
                            light.on = False

 

I hope it’s useful to you 🙂

One thought to “Keeping state of Philips Hues in Appdaemon”

  1. Great information – thank you. My problem is similar – but different :-). We seem to be having power outages – for just a few seconds – quite frequently where I live. My issue is that the Hue lights (white) come on after the outage and stay on till someone notices (wakes up, gets home etc.) If I want to use the lights for security purposes when I’m on holiday I want to be pretty sure that off means off and stays off.

    I suppose I could construct something perhaps to run on e.g. a Raspberry Pi – which does something similar to this – but perhaps polls less often – and records/writes the state (e.g. every 10 minutes). My problem would be knowing (guessing) that there’s been a power outage (repeatedly log current time, check against previous – if there’s a gap of e.g. 20 seconds = outage?)

    Also when you mention creating a new user on the bridge – would that be needed after a power cycle?

    Thanks for the general ideas and information.

Leave a Reply

Your email address will not be published.