Google Search

Custom Search

Powered by...

Find us on Facebook

Monday, 20 April 2015

KodiRemote for Micasaverde Vera Home Automation - Part 2

In part one here I discussed the installation and setup for the newer project called KodiRemote for integrating Kodi Media Center and the Micasaverde Vera Z-wave controller. This is based on the older project XBMCState.
In part two I will look at creating some scenes in Vera and using the playback status updates sent from Kodi to Vera to automate some lighting and curtain control. In Vera I created three new scenes.
  • Kodi – Close Curtains
  • Kodi – Lounge Dim 25%
  • Kodi – Lounge On
image

Scene – “Kodi – Lounge Dim 25%”

On the Triggers tab of this scene I created four new triggers. I named them as follows:
  • Kodi – Video Resumed
  • Kodi – Video Starting
  • Kodi – Music Starting
  • Kodi – Music Resuming
image

So if any of these triggers are detected the lights will be dimmed down to 25%.

If we look at one of the triggers we can see in more detail how it works.

Under the “Device” field you select the KodiRemote device instance you would like to configure. As these scenes are for the Kodi HTPC in my Living Room I selected the #113 “Kodi01 – KodiRemote” device from the device list.

In the “What type of event is the trigger?” Select “Player State Changes”.

In the “Name for this trigger” field give the trigger a suitable name, in this example I called it “Kodi – Video Resumed”.

In the “Player State” field you select the playback state of Kodi, in this example I selected “Video is resumed”.

This means that when Video is resumed on the Kodi Media Center PC, this Vera scene with this trigger will then perform some action.

image

Here is another screen shot of the “Kodi – Music Starting” trigger.

image

So once you have created all your triggers we then need to complete the scene creation by adding our actions that Vera will carry out.

In my case I have some LUUP code that should be run when either one of the four triggers are detected.

image

I have also created a Virtual Switch in Vera using the Virtual Switch plug-in from the MiOS Marketplace here.

I named the Virtual Switch “Link Kodi to Devices?”
image

If the Virtual Switch is turned ON then Kodi Media Center is linked to the three scenes.
  • Kodi – Close Curtains
  • Kodi – Lounge Dim 25%
  • Kodi – Lounge On
If the Virtual Switch is turned OFF then Kodi Media Center is not linked to the three scenes and the automation of dimming or brightening the living room lights or automatically closing the curtains, will not happen.

E.G. By using a Virtual Switch such as this one, you can turn on and off the integration between Kodi and Vera which is very useful, as you might not want it to happen all the time.
image

In the name field enter a suitable name for the Virtual Switch.

image

In the Text1 field I entered the text “Linked = ON”.

You also need to make a note of the Virtual Switches device ID number in Vera? As we will use this in our scenes LUUP code.

image

Here is the Luup code from my “Kodi – Lounge Dim 25%” scene.

Basically it does the following:

1. Checks first to get the current status of the Virtual Switch with the device ID number 92. Is it on or off? 1 is ON 0 is OFF.

2. If that Virtual Switch device is currently turned ON then..

3. Run another scene to dim the lights 25%, in this case it runs my scene number 5 which is just a regular scene that dims the lights to 25%.

4. If the Virtual Switch was turned OFF then it would just end and not do anything.
   1: Link_XBMC_to_devices = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",92)

   2: if(Link_XBMC_to_devices=="1")then

   3: luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{SceneNum="5"},0)

   4: end
Here is the “Kodi – Lounge On” scene triggers.
  • Kodi – Video Paused
  • Kodi – Music Stopped
  • Kodi – Music Paused
  • Kodi – Video Ended
  • Kodi – Video Stopped
image

So if any of these triggers are detected then the LUUP code in the “Kodi Lounge On” scene will be run.

image

1. Checks first to get the current status of the Virtual Switch with the device ID number 92. Is it on or off? 1 is ON 0 is OFF.

2. If that Virtual Switch device is currently turned ON then..

3. Run another scene to turn on the Lounge lights, in this case it runs my scene number 1 which is just a regular scene that turns on the lounge lights to 100%.

4. If the Virtual Switch was turned OFF then it would just end and not do anything.

   1: Link_XBMC_to_devices = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",92)

   2: if(Link_XBMC_to_devices=="1")then

   3: luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{SceneNum="1"},0)

   4: end
So with these two scenes and the triggers that have been added to each and if the Virtual Switch is turned ON, then for example if I start playback of a movie in Kodi Media Center, the lounge lights will automatically dim down to 25%. And if I then pause or stop video playback the lounge lights will automatically brighten up to 100%.

Obviously we only want this to take place when its actually night time, this is where the Day or Night plug-in for Vera we installed in Part one comes in to play.

In the KodiRemote (Micasaverde Kodi Event) add-on for Kodi in the settings there, we need to specify that the various playback states are set to “Night”.

image

[image%255B152%255D.png]

And finally I have a third scene in Vera for my Z-wave curtain rail.

I only have one trigger in this scene and that is “Kodi – Video Starting”.

Basically this scene closes the curtains if it is currently night? and if the curtains are still open? and if I start to play video in Kodi Media Center, then the curtains will be automatically closed.

image

Here is the LUUP code for the “Kodi – Close Curtains” scene.

image

1. Checks first to get the current status of the Virtual Switch with the device ID number 92. Is it on or off? 1 is ON 0 is OFF.

2. If that Virtual Switch device is currently turned OFF then just end and do nothing.

3. Checks to see if the curtains are currently opened or not? Device ID #107 is the device ID number in Vera for my Fibaro Blind Controller module.

4. If the curtains are open (which is 100) then..

5. Run another scene to close the curtains, in this case it runs my scene number 138, which is just a regular scene that closes the curtains.

6. End.
   1: Link_XBMC_to_devices = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",92)

   2: if(Link_XBMC_to_devices=="0")then return false end

   3: are_curtains_open = luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",107)

   4: if(are_curtains_open=="100")then

   5:      luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{SceneNum="138"},0)

   6: end
These are the three regular scenes in Vera, that the Kodi automation scenes run when all the conditions are met. And these scenes ID numbers are the ones referenced in the various LUUP code above etc.

image

Summary

Well I hope this all makes sense as its quite hard to explain. But with the KodiRemote add-ons for Kodi Media Center and Vera, you can integrate the two together and have Vera perform actions based on Kodi’s current media playback status. I am sure there are various ways of setting up your scenes in Vera, I’m for example not using PLEG and tend to just add the LUUP code myself. But as long as you know how to create the triggers with the KodiRemote devices, then you can go about creating the actions for your scenes as you normally would.

Here is a short video clip showing "Video Starting" playback in Kodi. I then pause and resume the video a few times. The video quality isn't great, but you get the idea if you watch it.



10 comments:

Richard Godfrey said...

This looks great. Is there any way to create a virtual switch so that Vera knows whether you are navigating the Movies, TV Shows or EPG? You could then fire the scenes, say, after you have browsed either movies or tv show menus but not the EPG.

A big bug bear with the old XBMCState plugin is that it didn't differentiate between playing a movie or watching live tv. I don't want to dim lights etc whilst watching tv, but I do when watching a movie.

CW-KID said...

I don't see why not, you could create scenes to turn on a virtual switch when curtain areas of the Kodi GUI are accessed and triggered etc, should be possible.

CW-KID said...

Although thinking about it, you wouldn't be able to use the triggers for Video Starting / Video paused etc. But you could possibly for example upon entering the Videos area of Kodi have the lights dim down and upon exiting that area, say returning to the Home menu, have them brighten up again automatically.

These are the Kodi state triggers available:

Kodi Open
Menu Home
Menu Program
Menu Picture
Menu Setting
Menu Video
Menu Music
Menu Weather
Kodi Close

I don't think you'd need a virtual switch either as you'd just have your Kodi lighting scenes be triggered directly by any of the above Kodi states.

CW-KID said...

I've just tested the "Menu Video" trigger in the add-on for Kodi.

When I enter either the Movies library or the TV Shows library in Kodi the KodiRemote state changes OK to VIDEO.

However if I enter the Live TV EPG then it does not change to VIDEO so it works in that respect, so I think this would be possible.

CW-KID said...

However I don't know what exit trigger you could use?

As "Menu Home" will trigger any time you go back to the home menu, no matter which area of the Kodi UI you have been in.

So I can see that you could have a scene run when you enter the Movie and TV Show library areas, no problem.

But if you wanted another scene to run when exiting just these two areas, then I am not sure how you can do that.

Richard Godfrey said...

I've played with that as well. But there are a few snags.

When playing a movie if I pause then ideally the lights would come up and go back down on resume. However this would also trigger when pausing/resuming live TV.

I've had a look at enabling custom entries in the Kodiremote plugin when certain Window IDs are launched. The EPG for example is Window ID 10617. So I can send a custom entry for that to Vera, but I am unable to trigger anything when the Kodi state changes to the custom event. This is where I thought a virtual switch would come in useful. On for when "Menu Video" is activated and off for when "Custom Entry 1" is activated. That way, by navigating to EPG, it disables any light action (probably need to use Pleg or Luup code).

Sure we can get there, my gut feel is a tweak to Vera plugin to allow Kodiremote custom event triggers....??

CW-KID said...

Ok I see what you mean and sounds like a good idea! So when you enter the EPG area, have this turn off the virtual switch and therefore the lights then won't auto dim and brighten. Presumably you then need other "Menu" triggers to turn the switch back on, say when entering movies or tv shows areas.

I never used the custom events, does it work? When you enter the EPG does the status of the KodiRemote device in Vera change and reflect this?

Then you'd need to be able to select this custom event as a trigger in your scene to turn off the virtual switch, which I take it you can't do currently?

In which case the developer will need to look in to adding custom event triggers as you say!

CW-KID said...

Ok I can also confirm that the custom events in the Kodi add-on work and when I enter the EPG (Window ID 10617) the KodiRemote device in Vera changes to event_1

However looking in the scene triggers in Vera there appears to be no way to select event_1 as a trigger.

Richard Godfrey said...

Yeah that's the problem, you can see the custom event appearing in the plugin, you just cant trigger anything from it.

I saw your post in the Kodi forum related to the Kodiremote forum. Maybe we could pursuade him to add it as a feature!

CW-KID said...

Yes you are correct and using your idea, I see two ways this could be done, both need the Vera plug-in updating!

1. Add triggers for custom events, as we discussed, this should be done regardless really.

2. Add more "Menu" status items to cover the Live TV areas of the Kodi GUI. E.g.

"Menu EPG"
"Menu TV Channels"
"Menu Radio Channels"
"Menu Recordings"
"Menu Timer"

Number 2 would be my preferred option as that would leave the custom events unused and they could then be used for something else.