[SOLVED] Hue Dimmer Switch

Can you just use the Dimmer switch to send commands to OH i understand you need too use the HTTP Binding ?

can you setup the app select buttons do nothing but oh receives the button press and does something ?

i have been curious about this also! I wish the binding had the dimmer included.

Did you see my post Hue Binding - Dimmer Switch in the linked thread?

It´s quiet easy to use it, you’ll need two kinds of items: one is the timestamp of the remote and the other is the button event. So your rule is gonna watch for changes of the update timestamp and then its looking for the button ID and then execute

1 Like

You’ll find a oh native solution here: HowTo: Use Philips Hue sensors (Motion Sensor / Dimmer Switch)

And yes, even if the hue app shows the dimmer (or tap in my case) as “Not configured” it’ll still record and publish the buttons pressed and times that was done.

That said, I don’t like the suggested (very long, IMO) latency of the oh solution. Since it burdens oh with pulling the value it doesn’t run too often. Instead I have a python script pulling the stuff as fast as it can (but you can add a sleep command, if it turns out blocking anything on your system, increasing latency), and then pushing it to oh via mqtt. In my case, this adds about 1% load on a raspi running this, the mqtt broker itself, and a number of other services, but oh is running on another, dedicated raspi.

#!/usr/bin/python3
import json
import urllib.request
import paho.mqtt.client as mqtt

mqtt_broker="<url/ip of broker>" 
mqttc = mqtt.Client()
mqttc.connect(mqtt_broker, 1883,  keepalive=60)

# List of tap and dim sensor numbers
tapdim_sensor=(7, 9, 55, 60, 61)
tapdim_lastupdated_old=["0:0:0", "0:0:0", "0:0:0", "0:0:0", "0:0:0"]
tapdim_lastupdated=["0:0:0", "0:0:0", "0:0:0", "0:0:0", "0:0:0"]

while True:
    mqttc.loop() # Keep connection to broker alive
    # Get json data from bridge
    with urllib.request.urlopen("http://<url/ip of bridge>/api/<key>/sensors/") as url:
        data = json.loads(url.read().decode())

    i = 0
    for n in tapdim_sensor:
        tapdim_lastupdated[i] = (data['%d' % n]['state']['lastupdated'])
        if tapdim_lastupdated[i] != tapdim_lastupdated_old[i]:
            mqttc.publish('huesensor/%d/lastupdated' % n, payload=tapdim_lastupdated[i], qos=0, retain=False)
            mqttc.publish('huesensor/%d/buttonevent' % n, payload=data['%d' % n]['state']['buttonevent'], qos=0, retain=False)
            print ("TapDim:", tapdim_sensor[i],tapdim_lastupdated[i],data['%d' % n]['state']['buttonevent'])
            tapdim_lastupdated_old[i] = tapdim_lastupdated[i] 
        i = i + 1

Note: This is a tuned down version, the full script handles as well presence sensors including the Philips hue geofence, battery states, temperatures and light levels, so it polls a bit less often than this one.

In oh the values are then added via mqtt binding like:

DateTime  Hue_Tap_01_event        (gLiving_Tapevent)   {mqtt="<[raspi-serverMQTT:huesensor/9/lastupdated:state:default]"}
Number  Hue_Tap_01_key          (gLiving_Tapkey)     {mqtt="<[raspi-serverMQTT:huesensor/9/buttonevent:state:default]"}

Thanks everyone for your posts

I purchased 3 dimmers for £40 while they were on offer yesterday workes perfect using the http binding the same as the motion sensor

I used this

This is truly a great piece of code. I implemented this with my 6 Hue Dimmer switches and it works very, very well. For being a “non native” solution one cannot ask for more. The delay is down well below a second and is stable.

I have made some minor additions and observations which I though that I would share. Many are probably obvious for the author but could help someone else.

the first is that if you like me depend on authorisation for MQTT then insert a row:

mqttc = mqtt.Client()
mqttc.username_pw_set("my user here", password="my password here")
mqttc.connect(mqtt_broker, 1883,  keepalive=60)

the second observation is that if you wish to run this with a single dimmer switch,

tapdim_sensor=(7)

will fail! use instead:

tapdim_sensor=[7]

In my case I use the dimmer switches for the control of 4 different “scenes” in the room. The rule I have written to accomplish this looks like this:

when
	Item Hue_Switch_01_event changed
	or Item Hue_Switch_01_key changed
then
	if (Hue_Switch_01_key.state < 1005) {
		sendCommand(CONTROL_ITEM, 0)	
            } else if (Hue_Switch_01_key.state < 2005)  {
                sendCommand(CONTROL_ITEM, 1)
            } else if (Hue_Switch_01_key.state < 3005)  {
                sendCommand(CONTROL_ITEM, 2)	
            } else {
            	sendCommand(CONTROL_ITEM, 3)	
            }
end

Some notes about this. Depending on the “Hue_Switch_01_event” for change detection enables having multiple Dimmer switches in the same room controlling the same “scenes” (as one may be used to turn it on and the other to turn it off). However, initially I had only this for detection and experienced erratic behaviour where it was not triggering the rule. I would thet sometimes the event change is not logged to the MQTT while the key change is. Thus the one of an “OR” statement solves this.
The “if” commands merge all the possible tabs per button as I find that different users in the house press them with different duration thus triggering different codes. 4 states are sufficient for us and it is less confusing for the user.

Finally I can also comment on the resource use for this python script. I run my smart home setup in an Intel Nuc running a plethora of LXC containers under Proxmox. This is an amazing collection of tools and works brilliantly. Worth considering if you are setting up a new system and would like scalability far beyond a Raspberry Pi. Anyway, running this script In a separate LXC container is taxing a single core of the CPU less than 0.1% and is using 28 MB of RAM. Thus it is a trivial additional lode on any system.

I hope this is of help.

Thanks!

I also found the hue sometimes does not respond properly to the

with urllib.request.urlopen

To avoid the script crashing at this point put this inside a try-except structure.
Best regards,
Rivi

Hi guys,

just to understand this correctly and if I need this code as a solution for my problem.
I recently bought a Ceiling Yeelight and have 3 additional hue lights in my room which I am switching with a dimmer switch. The goal is to use the dimmer switch also to turn on/off the ceiling lights.
I did discover that the hue dimmers are not exposed natively therefore we can access it with the above code.

BUT I thought I would just create a rule for the other hue lights when the receive a command, they switch on/off the ceiling yeelight because I thought although OH does not natively gets a response from the dimmer directly, it does get that the light has changed its state and therefore reacts to my rule.

Unfortunetely that does not work althought in the console log, obviously those hue lamp status changes get recognized by OH.

So I need to implement the above code for my case to work then? Seems strange or am I missing something here?

Thanks in advance!
Henning

What you describe, using lamps status as triggering item for another rule, should work conceptually IMO, but there are caveats. First, there might be some delay between lamp switched off by hue bridge and that being propagated to oh. Second, at least in my installation dropouts in which connection is lost between hue bridge and lamp for some seconds is not that rare. That might cause hiccups.

I am not at home for another week, so i can’t play with the installation, but i think the simple approach you had first should be possible to make work.