Jump to content

Welcome, Guest!

Be a part of CinemaVision today! Once registered and logged in, you will have access to public chat and your own private messenger, you can view and contribute guides, collaborate on the forums, review downloads, give reputation to your fellow members, contribute content and so much more. Registering is quick and completely free, so what are you waiting for?
   Sign In    Sign Up

Become a RedCarpet Club Member Today!

   Join Now
  • 0
DexDeadly

OpenHAB - Advanced Dimmer Control using JSON

Question

Here is a more advanced way to dim the lights in your home theater room using OpenHAB and CinemaVision using the action files.  With this setup you can control how quick or slow your lights will dim and to what percentage of brightness with a chosen value between 0 - 100.  This requires edits to the items file and the rules file within OpenHAB.

The items file is a simple edit.  Simply add the following line:

String HT_Dimmer "HT Dimmer Data [%s]"

The rules file we will be adding a new rule to handle the JSON data we will send to it in the action file.  Place this rule anywhere you would like within the file.  I have placed instructions and explanations within the comments of the rule for those wanting to understand what's occurring. 

/* CinemaVision Customizable Dimmer */
rule "Home Theater Dimmer"
    when
        /* Trigger the rule when HT_Dimmer receives the information via JSON */
        Item HT_Dimmer received command 
    then
        /* Set current state of your dimmer in the home theater.  This can be a single dimmer attached to one light you have in the room or a group dimmer like in this example that controls all the lights in the room */
        var Number curstate = 0
        if(Light_LivRoom_AllLights_Dim.state instanceof PercentType) curstate = Light_LivRoom_AllLights_Dim.state as PercentType
        
        /* This will take the JSON we send from the action file within CinemaVision and parse out our configuration parameters */
        var String json = (HT_Dimmer.state as StringType).toString
        
        /* Example JSON being sent {"_type": "dimDown", "DimPercent": 70, "increments": 5, "speed": 500} */
        var String type = transform("JSONPATH", "$._type", json)
        
        /* Place an entry to the log to verify we are receiving a value for the type parameter being sent */
        logInfo("HT Rule", "JSON Information " + type)
        
        /* If the type sent via JSON is dimDown execute this block of code */
        if (type == "dimDown") {
        
          /* Place an entry to the log to verify we started the dimDown Loop
          logInfo("HT Rule", "Starting dimDown Loop")    
          
          /* set the variable dimto to the percentage we want to dim down to and output to the log to make sure the information was in fact saved.  Using example we should have this set to 70. */
          var dimto = new Integer(transform("JSONPATH", "$.DimPercent", json))
          logInfo("HT Rule", "dimto variable = " + dimto)    
          
          /* set the variable inc to the percentage we want dim each time the loop runs. and output to the log to make sure the information was in fact saved.  Using example we should have this set to 5. */
          var inc = new Integer(transform("JSONPATH", "$.increments", json))
          logInfo("HT Rule", "inc variable = " + inc)
          
           /* set the variable speed to the amount of time in milliseconds to wait before running the while statement again and output to the log to make sure the information was in fact saved.  Using example we should have this set to 500 which is half a second. */
          var speed = new Integer(transform("JSONPATH", "$.speed", json))
          logInfo("HT Rule", "speed variable = " + speed)
          
          /* Output to the log our current dimmer state to make sure this variable is set as well */
          logInfo("HT Rule", "curstate = " + curstate)

          
          while(curstate > dimto)

            {
                /* Take the current state of the dimmer and subtract are percentage increments */
                curstate=curstate - inc
                
                /* Send the new current state to our dimmer */
                sendCommand(Light_LivRoom_AllLights_Dim, curstate)
                
                /* Wait the time we choose in milliseconds before running the loop again */
                Thread::sleep(speed)
            }
            
        }
        /* This is the same as the above code block only this time we'll be increasing the lights. */
        if (type == "dimUp") {
        logInfo("HT Rule", "Starting dimUp Loop")    
          var dimto = new Integer(transform("JSONPATH", "$.DimDownPercent", json))
          logInfo("HT Rule", "dimto variable = " + dimto)    
          var inc = new Integer(transform("JSONPATH", "$.increments", json))
          logInfo("HT Rule", "inc variable = " + inc)
          var speed = new Integer(transform("JSONPATH", "$.speed", json))
          logInfo("HT Rule", "speed variable = " + speed)
          
          logInfo("HT Rule", "curstate = " + curstate)          
          while(curstate > dimto)

            {
                curstate=curstate - inc
                sendCommand(Light_LivRoom_AllLights_Dim, curstate)
                Thread::sleep(speed)
            }
            
        }
        
        /* Set the HT_Dimmer state to the string done and output to logfile */
        logInfo("HT Rule", "Successful execution of Home Theater Dimmer rule!")
        postUpdate(HT_Dimmer,"Done")
        
end

**NOTE: I have found that if you are using the OpenHAB Designer in Ubuntu, the transform triggers will underline in red saying this is not correct.  However this is not true and the rule will work just fine.  It seems this occurs with the designer sometimes and its being looked at.  Also if you do not want to flood your logfile you can remove the logInfo lines from the code.  I placed these here during development to ensure everything was being passed along correctly and left in to help any troubleshoot any issues you may have.

The last bit we need to do is create our action files.  For me I have 4 text files labeled where I have them triggering in my sequence.  Example, I have a text file called theater_lights_triva.txt which triggers before my triva slides.  I've found it easier using this naming convention for easy of updating.  Here is how you would format your action file in order to properly send over the JSON information. Please note that JSON is CaSe SeNsItIvE.  

http://INSERT_OH_IP:INSERT_OH_PORT/rest/items/HT_Dimmer - This is the REST API string of the Dimmer we created in the items file. Replace the red text with your OpenHAB servers IP and Port
{"_type": "dimDown", "DimPercent": 70, "increments": 5, "speed": 500} - This is the JSON String that gets parsed by our rule within OpenHAB. See below breakdown

_type = This value can be either dimDown or dimUp depending on if you want to dim your lights up or down within the sequence.
DimPercent = This is going to be the percentage of the dimmer we want to be at when the rule triggers
increments = This is the percentage value you would like to dim by
speed = This is how long to wait in milliseconds before dropping by the next increment.

** NOTE: If you have security enabled within OpenHAB you will need to add an additional line to your action file.  The 2nd line in the action file will need to be the following:
HEADERS: {"Authorization": "Basic=XXXXX"}  - You can generate the Basic=XXXX by using the Postman plugin for Chrome. Using Postman you can place in your user credentials and the header line will generate for you.  You can than place this within the header line and you will than be able to send your JSON request lines with authorization turned on.  Here is an example of how the action file can look 

http://INSERT_OH_IP:INSERT_OH_PORT/rest/items/HT_Dimmer - This is the REST API string of the Dimmer we created in the items file. Replace the red text with your OpenHAB servers IP and Port
HEADERS: {"Authorization": "Basic=XXXXX"} - Authorization header for using with an OpenHAB server with security enabled.
{"_type": "dimDown", "DimPercent": 70, "increments": 5, "speed": 500} - This is the JSON String that gets parsed by our rule within OpenHAB. See below breakdown

This information is also explained in the rule comments but here it is again in a more compressed form.  Save the file and place in your actions folder on your Kodi system where you have the CinemaVision folders.  Then within the Sequence editor add the action.  In the example above I placed this right before the trivia slides start.  Once you place the action in, point it to your newly created .txt file.  Save your sequence and run it, your lights should now react within the sequence.

Feel free to ask any questions or if you find anything that needs tweaked let me know!

 

 

Edited by DexDeadly

Share this post


Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Hi I have a json url that when executed switches off my lights, now I have no clue where shall I put that in cinemavision config to get it excuted? can you assist.

Share this post


Link to post
Share on other sites
  • 0

Hello @sam9s !  You go ahead and trigger this by editing your sequence as mentioned above.  You can find the guide to editing your sequence here:

If you still need assistance please feel free to reach out to me via PM.

Share this post


Link to post
Share on other sites
  • 0

Hi there,

i would like to turn an openhab item called "Zigbee" OFF when i am using Cinemavision.

The RGB Lightstripe is in a Philips HUE Bridge by a  Zigbee Controller from 3rdParty, but it didn't turn it off while the Hue LightBulbs turned off.

that's why i want to try turning it off using Cinemavision.

I have an item that works in openhab, but how can i get it work with Cinemavision ?

 

First i tried :

http://192.168.178.33:8080/CMD?Zigbee=OFF

no response

then looking at this thread here

if i use this in my Browser

http://192.168.178.33:8080/rest/items/Zigbee/

it shows

{"link":"http://192.168.178.33:8080/rest/items/Zigbee","state":"ON","editable":false,"type":"Switch","name":"Zigbee","label":"Kino RGB Lichter","tags":[],"groupNames":["gKino"]}

but what i have to do to switch the Zigbee Icon to OFF or ON

after using the openhab.txt i get this message in Cinemavision

ACTION ERROR LINE1: nfs://192.168.178.42/nfs/public/actions/openhab.txt
'\xref\xbb\xbfhttp://192.168.178.33:8080/rest/items/Zigbee'
Unrecognized command protocol: \xref\xbb\xbfhttp'

Skipped DUE TO ERROS

 

whats wrong or whats missing ?

 

i didn't understand using  this the postman plugin for chrome for the HEADERS

 

greets 

Orakel

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
  • 0

@DexDeadly I am trying to set up a more simplified version of this and its giving me trouble. I have a rule already made in Openhab that triggers all the lights and a blinds so when i watch a movie I can turn everything off. That rule is connected to a simple switch.  Can you advise how to put the following item json cURL into the right format for Cinema Vision? Any help would be greatly appreciated. I was able to reformat it for the Action to validate it and say OK in test mode but there were errors. 

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://myopenhabip:8080/rest/items/movie_lights"

 

# THIS IS THE SECOND ACTION IN MY ACTION FILE
# The following is the Execution Line
http://myopenhabip:8080/rest/items/movie_lights
# The following is the Data Line
PUT: {"on":true}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Guidelines and Terms of Use.