Python script for full rgb + brightness control of easyESP neopixel
Posted: Saturday 03 November 2018 21:16
I was trying to control an easyESP neopixel from an RGB switch but the best I could do was switch on a pre defined colour and brightness by sending the Neopixel command from the on/off action command as below.
I tried some blocky code but could not find out how to get the current colour and brightness levels.
So after some googling I wrote a python script that is run on the on/off command. And it works even better than I hoped, as whenever you make a change on the colour wheel or brightness it also runs the script, it must count as an "on" command on a change.
Here is my script. It is saved in domoticz/scripts/python. Make sure you chmod +x the file so it runs. my file is called owl2.py as it is a nightlight that sits in a printed owl body. The script queries Domoticz to get the current state of the switch by ID (97 here). Parses out the json to get the colours, brightness and the on/off state. Then builds a url string to change colours on the eastESP device. I also have a HA bridge running so this will all work with Alexa.
The onAction command is
Code: Select all
http://espeasyIPaddress/control?cmd=NeoPixelAll,255,255,0
http://espeasyIP/control?cmd=NeoPixelAll
So after some googling I wrote a python script that is run on the on/off command. And it works even better than I hoped, as whenever you make a change on the colour wheel or brightness it also runs the script, it must count as an "on" command on a change.
Here is my script. It is saved in domoticz/scripts/python. Make sure you chmod +x the file so it runs. my file is called owl2.py as it is a nightlight that sits in a printed owl body. The script queries Domoticz to get the current state of the switch by ID (97 here). Parses out the json to get the colours, brightness and the on/off state. Then builds a url string to change colours on the eastESP device. I also have a HA bridge running so this will all work with Alexa.
Code: Select all
#!/usr/bin/env python
import requests
import ast
ID = '97'
domo_url = "http://<username:password>@domoticzIP:port/json.htm?type=devices&rid={}".format(ID)
owl_control_url = 'http://easyESPIPaddress/control?cmd=NeoPixelAll,{},{},{}'
def app():
data = requests.get(url=domo_url)
colour_data = ast.literal_eval(data.json()['result'][0]['Color'])
level_data = data.json()['result'][0]['Level']
state = data.json()['result'][0]['Data']
red = int(colour_data["r"])
green = int(colour_data["g"])
blue = int(colour_data["b"])
level = int(level_data)
redl = int((red/100) * level)
greenl = int((green/100) * level)
bluel = int((blue/100) * level)
if state == 'Off':
redl = 0
greenl = 0
bluel = 0
#send to light
_i = requests.get(url=owl_control_url.format(redl,bluel,greenl))
if __name__ == "__main__":
app()
Code: Select all
script:///volume1/@appstore/domoticz/scripts/python/owl2.py