#!/bin/bash
# Script to update X10 dimmer module status in Domoticz when the level has been changed manually
#
# Adjust the value assigned to IDX to reflect the module idx number in Domoticz
# Adjust the value assigned to ADDR to reflect the module house and device code
#
IDX=8
ADDR=B1

# get the module's current dim level
LEVEL=`sudo -u pi heyu dimlevel $ADDR`

# debugging messages
#echo $LEVEL >> debug_dim.log & 
#curl "http://192.168.0.22:8080/json.htm?type=command&param=addlogmessage&message=Updating%20B1%20to%20level%20$LEVEL"

## These  will switch the lamp off and on & set the dim level at the same time probably not the way to go
##curl "http://192.168.0.22:8080/json.htm?type=command&param=udevice&idx=8&nvalue=0&svalue=$LEVEL"
##curl "http://192.168.0.22:8080/json.htm?type=command&param=udevice&idx=8&nvalue=1&svalue=$LEVEL"

# Update the Domoticz user variable holding the dim level. This MUST be done before updating the device to avoid action
curl "http://192.168.0.22:8080/json.htm?type=command&param=updateuservariable&vname=Bright_Level_$ADDR&vtype=0&vvalue=$LEVEL"

# This will update the slider (svalue) of the Domoticz device, but when the lua script is called, there will be
# no action, the calculated change in dim level, delta, will be 0.
curl "http://192.168.0.22:8080/json.htm?type=command&param=switchlight&idx=$IDX&switchcmd=Set%20Level&level=$LEVEL"


