Rule to shutdown Remote Raspberry Pi, then turn relay off via MQTT with delay

Tags: #<Tag:0x00007faf9aa11250> #<Tag:0x00007faf9aa11098> #<Tag:0x00007faf9aa10f30>

so a brief description of what I’m trying to accomplish here. What i would like is to have a rule that when a switch is pressed it sends the command to shutdown my remote RPI (there is 6 and these actually run motionEye), then wait for a period of time to allow for proper shutdown then send command via MQTT to turn off my relays. currently i have items for my relays working with mqtt and i also have an Item and Thing to shutdown my RPI implemented and working, but i dont know exactly how to write all this in a rule. Can i add the rule to the existing relay switches? or do i need to create new switches to something like dummy switches and put all the info in the rule? Also some of the RPI’s will be shutdown automatically at night do i need to have 2 rules or can i incorporate this into the rule itself. if someone could help get me goin in the right direction that would be awesome.

here is my items and things

Switch MERelay1 "MotionEye Relay [%s]" { mqtt=">[openhab:home/gr/sb/relay/1:command:OFF:1],>[openhab:home/gr/sb/relay/1:command:ON:0],<[openhab:home/gr/nb/relay/1:state:JS(WSNumber.js)]" }
Switch MERelay2 "MotionEye Relay [%s]" { mqtt=">[openhab:home/gr/sb/relay/2:command:OFF:1],>[openhab:home/gr/sb/relay/2:command:ON:0],<[openhab:home/gr/nb/relay/2:state:JS(WSNumber.js)]" }
Thing exec:command:MEFYDay [command="sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now", interval=0, autorun=false]

First attempt.

rule "Raspi Shut Down"
when
	Item switchShutDown changed to ON
then
	swSD.postUpdate(ON)
	Thread::sleep(1000)
	MERelay1.postUpdate(OFF)
	MERelay2.postUpdate(OFF)
end

Post all your items what you need for that.

ok so i do need to create a seperate “dummy” Item for the rule and then add my items to the rule to speciify action. i think i get it a little bit more now. I will be trying this and will update my progress. thank you for the quick response

so i dont think I was very clear about what i wanted i was able to get that working easily but i am not able to control the relay (without multiple buttons switches). completely disregard the second relay for now rule will be for 1 relay and 1 remote Pi. what im trying to get is a working switch hooked up to mqtt that i can turn on. then when i turn it off it will execute the rule to shutdown the remote Pi then after 30 seconds kills the power to the relay(which is hooked up to an arduino via MQTT). i tried to incorporate this rule into my current item, but i think the MQTT action on the item is taking place before the rule. I guess im truely not understanding the rules portion completely.

rule "motionEye Shut Down"
when
        Item MERelay1 changed to OFF
then
        val results = executeCommandLine ("sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now")
        Thread::sleep(30000)
        MERelay1.postUpdate(OFF)
end

Item MERelay1 changed to OFF

and

MERelay1.postUpdate(OFF)

doesn’t make sence.
If you want to have a long (30 seconds) delay, timer is the better alternative. But for the first stepp it works.

see im very confused!!! I tried to do a secondary items file to control the action, but for some reason it would not fire the rule to update the relay. I posted the rule i tried below.

How would i go about doing a timer function in the middle of the rule im fairly new to all of this and i dont quite understand. thank you for your time and patience.

rule "motionEye Control"
when
        Item MECam1 changed to ON
then
        MERelay1.postUpdate(ON)
end


when
        Item MECam1 changed to OFF
then
        val results = executeCommandLine ("sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now")
        Thread::sleep(30000)
        MERelay1.postUpdate(OFF)
end

and then my items

Switch MERelay1 "MotionEye Relay [%s]" { mqtt=">[openhab:home/gr/sb/relay/1:command:OFF:1],>[openhab:home/gr/sb/relay/1:command:ON:0],<[openhab:home/gr/nb/relay/1:state:JS(WSNumber.js)]" }
Switch MECam1 "motionEye Camera 1" 

Post the events.log please, to see what happened.

var Timer timer = null

rule "motionEye Control ON"
when
    Item MECam1 changed to ON
then
    MERelay1.postUpdate(ON)
    timer.cancel()
end

rule "motionEye Control OFF"
when
    Item MECam1 changed to OFF
then
    val results = executeCommandLine ("sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now")
    if(timer === null || timer.hasTerminated) {
        timer = createTimer(now.plusSeconds(30)) [|
            MERelay1.postUpdate(OFF)
            timer = null]}
end

I used your rule example and here is the log

2018-07-31 13:00:56.861 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'motionEye.rules', using it anyway:
The value of the local variable results is not used
2018-07-31 13:00:56.946 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'motionEye.rules'
==> /var/log/openhab2/events.log <==
2018-07-31 13:01:05.580 [ome.event.ItemCommandEvent] - Item 'MECam1' received command ON
2018-07-31 13:01:05.602 [vent.ItemStateChangedEvent] - MECam1 changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
2018-07-31 13:01:06.225 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'motionEye Control ON': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null
==> /var/log/openhab2/events.log <==
2018-07-31 13:01:12.236 [ome.event.ItemCommandEvent] - Item 'MECam1' received command OFF
2018-07-31 13:01:12.247 [vent.ItemStateChangedEvent] - MECam1 changed from ON to OFF
==> /var/log/openhab2/openhab.log <==
2018-07-31 13:01:12.258 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'motionEye Control OFF': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: motionEye.rules#|::0.2.1.2.0.0.2.6.1::0::/1)

ok im starting to get somewhere now i figured out i could not have a Thing and the rule firing together it was causing issues. i removed the Thing and i can now get the command to post now however there is a hang up at the end where it cycles the relay off then on again (and i do not hear the relay click at all)and i cant figure out why. I also added the changed from OFF to ON and it seemed to help with the errors, but not sure if this was necessary.

2018-07-31 14:23:33.021 [vent.ItemStateChangedEvent] - MERelay1 changed from NULL to ON
2018-07-31 14:23:33.070 [vent.ItemStateChangedEvent] - MERelay2 changed from NULL to ON
2018-07-31 14:23:33.126 [vent.ItemStateChangedEvent] - MERelay3 changed from NULL to ON
2018-07-31 14:23:33.176 [vent.ItemStateChangedEvent] - MERelay4 changed from NULL to ON
2018-07-31 14:23:33.220 [vent.ItemStateChangedEvent] - MERelay5 changed from NULL to ON
2018-07-31 14:23:33.262 [vent.ItemStateChangedEvent] - MERelay6 changed from NULL to ON
2018-07-31 14:24:20.057 [ome.event.ItemCommandEvent] - Item 'MECam1' received command ON
2018-07-31 14:24:20.072 [vent.ItemStateChangedEvent] - MECam1 changed from NULL to ON
2018-07-31 14:24:37.967 [ome.event.ItemCommandEvent] - Item 'MECam1' received command OFF
2018-07-31 14:24:37.980 [vent.ItemStateChangedEvent] - MECam1 changed from ON to OFF
2018-07-31 14:25:08.367 [vent.ItemStateChangedEvent] - MERelay1 changed from ON to OFF
2018-07-31 14:25:16.871 [vent.ItemStateChangedEvent] - MERelay1 changed from OFF to ON

Edit: so i restarted openhab a few times to get all the changes to take affect and i couldnt see the Thing in paper UI anymore which was right however when i was looking over the logs i noticed that when it reboots it still adds the item link how do i remove this. could this be causing my issue?

2018-07-31 14:23:17.541 [.ItemChannelLinkAddedEvent] - Link 'MEFYDay-exec:command:MEFYDay:run' has been added.

EDIT 2:
so found that i had to add a rule to change the state from NULL to ON for the new item, I also changed the “from OFF to ON” back to how you had it as i read this might be issue, but still cannot get the relays to actually switch and cant figure out why it reverts back to on after X time

2018-07-31 15:01:07.078 [vent.ItemStateChangedEvent] - MERelay1 changed from NULL to ON
2018-07-31 15:01:07.120 [vent.ItemStateChangedEvent] - MERelay2 changed from NULL to ON
2018-07-31 15:01:07.175 [vent.ItemStateChangedEvent] - MERelay3 changed from NULL to ON
2018-07-31 15:01:07.223 [vent.ItemStateChangedEvent] - MERelay4 changed from NULL to ON
2018-07-31 15:01:07.265 [vent.ItemStateChangedEvent] - MERelay5 changed from NULL to ON
2018-07-31 15:01:07.306 [vent.ItemStateChangedEvent] - MERelay6 changed from NULL to ON
2018-07-31 15:01:09.096 [vent.ItemStateChangedEvent] - MECam1 changed from NULL to ON
==> /var/log/openhab2/openhab.log <==
2018-07-31 15:01:09.111 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'motionEye Control ON': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null
==> /var/log/openhab2/events.log <==
2018-07-31 15:03:16.699 [ome.event.ItemCommandEvent] - Item 'MECam1' received command OFF
2018-07-31 15:03:16.713 [vent.ItemStateChangedEvent] - MECam1 changed from ON to OFF
==> /var/log/openhab2/openhab.log <==
2018-07-31 15:03:16.747 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now'
==> /var/log/openhab2/events.log <==
2018-07-31 15:03:46.776 [vent.ItemStateChangedEvent] - MERelay1 changed from ON to OFF
2018-07-31 15:03:54.544 [vent.ItemStateChangedEvent] - MERelay1 changed from OFF to ON
2018-07-31 15:03:54.563 [vent.ItemStateChangedEvent] - MECam1 changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
2018-07-31 15:03:54.572 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'motionEye Control ON': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null

ok , ok, ok…I finally got it functioning i totally missed that i had an item in another file that was linking it removed it now im am functioning the way i am supposed to seriously thank you for the help along the way today i totally would never have figure this out on my own.

there is only 1 thing idk if it is all that important, but im still throwing an error on the openhab log and it has to do with the Timer.cancel()

==> /var/log/openhab2/openhab.log <==
2018-07-31 17:02:09.646 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'motionEye Control ON': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null

I removed the Timer.cancel() from the rule and it removed the error, but i noticed that when i restarted it this time it still has the this, but yet again not affecting any action just more of a what is this and possibly how to remove or correct it?

2018-07-31 17:22:02.454 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'motionEye.rules', using it anyway:
The value of the local variable results is not used
var Timer timer = null

rule "motionEye Control ON"
when
    Item MECam1 changed to ON
then
    MERelay1.postUpdate(ON)
    if(timer !== null) {
        timer.cancel()
        timer = null}
end

rule "motionEye Control OFF"
when
    Item MECam1 changed to OFF
then
    val results = executeCommandLine ("sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now")
    if(timer === null) {
        timer = createTimer(now.plusSeconds(30)) [|
            MERelay1.postUpdate(OFF)
            timer = null]}
end
1 Like

@hr3 ok i tried that and it still did not change the outcome. so i was doing a little research about it and it has to do with something in the last rule and i believe it has to do with the “val results =” but i have tried changing a couple things and nothing seemed to work and i verified changed the “results” to “result” and it printed the change in the [info warning] (see below). yet again it works so dont care all that much and seriously thank you again for the help it was much appreciated.

2018-08-01 11:37:20.426 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'motionEye.rules', using it anyway:
The value of the local variable result is not used

2018-08-01 11:38:13.537 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'motionEye.rules', using it anyway:
The value of the local variable results is not used

That was you code, but if you do not need result change to

executeCommandLine ("sudo -u openhab ssh pi@10.0.0.189 sudo /sbin/shutdown now")
1 Like

oh wow…thank you!!!

i told you i was really confused! so simple! LOL im learning altho at a snails pace I will get this!