Author Topic: How I created a virtual motion sensor device  (Read 347 times)

Offline cw-kid

  • Hero Member
  • *****
  • Posts: 1509
  • Karma: +12/-2
How I created a virtual motion sensor device
« on: June 06, 2018, 09:58:25 am »
I've just been poking around the forums and from various threads worked out how to create a virtual motion sensor device. I am using a VeraPlus UI7.

Some background: I have a Fibaro Double Switch relay module connected to some bog standard PIR Security Flood lights in my back garden.

When these PIR flood lights detect motion from their inbuilt motion sensors they turn on. Reading the currently used wattage on the Fibaro Double Switch module, I can determine if the flood lights have come on, hence motion was detected. And when they automatically turn off again after a minute or so, the wattage on the Fibaro module will go back to 0 e.g no motion.

So I plan to use some PLEG logic to control the virtual motion sensor and if it is tripped or not etc.

Here is how I created my virtual motion sensor device in Vera.

1. Go to Apps - Develop Apps - Create Device

In the "Description" field enter a name for your new device, I used: "Motion Sensor (Virtual)"
In the Upnp Device Filename field enter: D_MotionSensor1.xml
Select a room for the device to be located in and click the create device button.

2. Upload the attached I_MotionSensor1.xml file to your Vera. This makes the Disarmed / Armed buttons work in the GUI for the virtual motion sensor device.

Go to Apps - Develop Apps - Luup Files

Click the Upload button and browse to the I_MotionSensor1.xml file to upload it.

3. Edit the Parameters of the new Virtual motion sensor device

Go in to the device then go to Advanced - Params tab

Scroll down and look for device_json and enter D_MotionSensor1.json in to the field and then press tab on the keyboard.
look for impl_file and enter I_MotionSensor1.xml in to the field and then press tab on the keyboard.

Also whilst in here make a note of the devices ID number in my case it was 287.

Go to Settings - Z-Wave Settings - Advanced tab and select Reload Engine hit the Go button and wait for the engine to restart.

4. Perform two actions to make the variables appear for Armed and Tripped.

a. First from the devices list in the web GUI find your new virtual motion sensor and press the Armed button from being Disarmed.

b. Run this command, go to Apps - Develop Apps - Test Luup Code (Lua) paste it in to the box and then click the Go button.

But change the DeviceNum from 287 to whatever the device number is of your new virtual motion sensor.

Code: [Select]
luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 1,287)
This should change the virtual motion sensors status to tripped and you will then see a Red icon on it.

5. Check what you did in step 4. worked? In the Vera web GUI in the browser hit the F5 or reload the browser page. Now go back in to the Virtual Motion sensor device and go to Advanced - Variables tab. You should now see some variables have been added "Tripped" and "Armed"

The values of these variables will be 0 for OFF (Not Tripped) or 1 for ON (Tripped) etc.

That's it the new virtual motion sensor device has been created.

Now you need to create a scene or some PLEG logic to trip / untrip the virtual motion sensor device, to suit your requirements.

« Last Edit: June 06, 2018, 02:10:56 pm by cw-kid »

Offline cw-kid

  • Hero Member
  • *****
  • Posts: 1509
  • Karma: +12/-2
Re: How I created a virtual motion sensor device
« Reply #1 on: June 06, 2018, 10:00:50 am »
This is how the new virtual device looks in the UI7 web GUI, see screen shots.

If I made any mistakes or missed anything? Let me know!  ;D

Offline cw-kid

  • Hero Member
  • *****
  • Posts: 1509
  • Karma: +12/-2
Re: How I created a virtual motion sensor device
« Reply #2 on: June 06, 2018, 11:44:28 am »
IGNORE THIS:

I seem to have hit a snag with my PLEG logic and setting the Tripped variable in my actions.

If I use this http command it works and the Tripped variable is set to 1 and the motion sensors icon changes to red OK.

Code: [Select]
http://192.168.1.100/port_3480/data_request?id=lu_variableset&DeviceNum=287&serviceId=urn:micasaverde-com:serviceId:SecuritySensor1&Variable=Tripped&Value=1
However if instead I try to send a luup.variable_set to the virtual motion sensor instead like:

Code: [Select]
luup.variable_set("urn:upnp-org:serviceId:SecuritySensor1", "Tripped", 1,287)
It seems to create a second variable called Tripped and sets it to 1 and the icon does not change to red.

So then I end up with two variables called "Tripped" see screen shot.

I guess I could use a wget to send the http command in the PLEG action but its not ideal.

Code: [Select]
luup.inet.wget("http://192.168.1.100/port_3480/data_request?id=lu_variableset&DeviceNum=287&serviceId=urn:micasaverde-com:serviceId:SecuritySensor1&Variable=Tripped&Value=1")
Anyone know why when using a luup.variable_set command named Tripped it creates a second Tripped variable ?

« Last Edit: June 06, 2018, 01:39:29 pm by cw-kid »

Offline cw-kid

  • Hero Member
  • *****
  • Posts: 1509
  • Karma: +12/-2
Re: How I created a virtual motion sensor device
« Reply #3 on: June 06, 2018, 12:09:46 pm »
OK here is my PLEG logic I just setup, I've not fully tested it yet as it not dark outside yet, but manually running the PLEG actions does change the Tripped status of the virtual motion sensor OK.

On the Fibaro Double Switch relay, I am using one side of it connected to a wall switch and to two PIR security flood lights, the other side of the relay is used for something else not related.

So in PLEG under device properties, I created a new one for the correct portion of the Fibaro Double Switch relay connected to my flood lights to monitor the current wattage.

For some reason when the lights are off but the relay is turned on, it says 0.6 watts are being used so I took that into consideration with my logic.

1. Device Property

Name   
FloodLights_Watts

Device Name
Flood Lights[278] 
       
Device Variable
Watts[urn:micasaverde-com:serviceId:EnergyMetering1]

Value
0.6

2. Conditions

I created two conditions one for ON aka Greater than 1W and one for OFF aka Less than 1W.

Name
cFloodLights_Watts_On                               

Logic Expression
FloodLights_Watts>1            

Comments: Device Status - If the current wattage of the Flood Lights is Greater than 1W

Name
cFloodLights_Watts_Off

Logic Expression
FloodLights_Watts<1         
   
Comments: Device Status - If the current wattage of the Flood Lights is Less than 1W

3. Logic Actions

I then created the two actions in PLEG. The one for when the PIR security lights have turned on sends me a Vera Alerts notification and also trips the virtual motion sensor.

The action for when the PIR security lights have turned off again, just un-trips the virtual motion sensor.

Name
cFloodLights_Watts_On

When
Condition is True with Repeats                                       

Defined Actions
Immediate - 2   

Comments: Device Status - If the current wattage of the Flood Lights is Greater than 1W then send Vera Alerts notification and trip the virtual motion sensor device

Name
cFloodLights_Watts_Off                           

When
Condition is True with Repeats                                       

Defined Actions
Immediate - 1   

Comments: Device Status - If the current wattage of the Flood Lights is Less than 1W then un-trip the virtual motion sensor device


cFloodLights_Watts_On.png and cFloodLights_Watts_Off.png are the two PLEG actions to set the tripped status on the virtual motion sensor using the LUA tab option and the correct command in each.

LUA code to trip the virtual motion sensor:

Code: [Select]
luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 1,287)
LUA code to un-trip the virtual motion sensor:

Code: [Select]
luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 0,287)
« Last Edit: June 06, 2018, 01:58:37 pm by cw-kid »

Offline jswim788

  • Hero Member
  • *****
  • Posts: 751
  • Karma: +51/-2
Re: How I created a virtual motion sensor device
« Reply #4 on: June 06, 2018, 12:17:15 pm »
You are getting different variables because you are using different SID's:
Code: [Select]
urn:upnp-org:serviceId:SecuritySensor1
urn:micasaverde-com:serviceId:SecuritySensor1
A unique variable is defined both by the SID and the variable name.

Offline cw-kid

  • Hero Member
  • *****
  • Posts: 1509
  • Karma: +12/-2
Re: How I created a virtual motion sensor device
« Reply #5 on: June 06, 2018, 12:20:55 pm »
You are getting different variables because you are using different SID's:
Code: [Select]
urn:upnp-org:serviceId:SecuritySensor1
urn:micasaverde-com:serviceId:SecuritySensor1
A unique variable is defined both by the SID and the variable name.

Well spotted! Thanks I didn't see that. I will try and correct it and update my first post.

Offline cw-kid

  • Hero Member
  • *****
  • Posts: 1509
  • Karma: +12/-2
Re: How I created a virtual motion sensor device
« Reply #6 on: June 06, 2018, 01:17:31 pm »
I have updated the posts above to reflect using the luup.variable_set command instead of the luup.inet.wget command.

Everything is working as expected now.

I then added the new virtual motion sensor device in to the Imperihome app, a widget on my "Back Garden" dashboard page and into a Composite security device that shows all my security sensors etc.

Obviously in my setup here, the virtual motion sensor will only work at night time, when the PIR security lights detect motion and turn on their light and the wattage on the Fibaro relay is then greater than 1W. But better than no motion sensor I guess.

I have a door sensor on the back gate anyway's so know if that has been opened. I just wanted to play around with a virtual motion sensor to see if I could do it or not !

« Last Edit: June 06, 2018, 01:28:24 pm by cw-kid »