Hi Guys,
here my little Guide / Scripts for start / stop and get the state of a service on your PI with a ShellSwitch.
Whats the Problem?
My problem wars that i use Z-Way on my Pi with Pimatic and today a waked up and my Zwave Motionsensor didnt work, so i checked pimatic and there it wars 6k Errors because Pimatic checks every second the state of my Montionsensor.
Solution:
A ShellSwitch that know the state of the service and can it start over a pimatic rule / manuell if its needed.
Scripts:
Important the “state-script” will also shown “false” if you enter a misspelled service name or not existing service name
The Scripts works all like this:
in Pimatic:
#Scriptpath Servicename
/home/pi/pimatic-scripts/service_start.sh z-way-server
in the console:
#Scriptpath Servicename
./service_start.sh z-way-server
The Script checks that you have entered a service name, if you not you will see this message in the pimatic errorlog:
error [pimatic-shell-execute]: [ShellPresenceSensor#test] Error: unknown state="no service name entered, check your device in Pimatic"!
“service_start.sh”
#!/bin/bash
service=$1
if [ -z "$service" ]
then
echo "no service name entered, check your device in Pimatic"
else
sudo service $service start
fi
“service_stop.sh”
#!/bin/bash
service=$1
if [ -z "$service" ]
then
echo "no service name entered, check your device in Pimatic"
else
sudo service $service stop
fi
“service_state.sh”
#!/bin/bash
service=$1
if [ -z "$service" ]
then
echo "no service name entered, check your device in Pimatic"
else
if state=$(pgrep $service) > 0
then
echo "true"
else
echo "false"
fi
fi
(this guide is only for Users that have Pimatic running with the User “Pi” because you need a User with “sudo” permissions, if you have this different you may have to make more permission work first)
- Create the 3 SH-files in your folder “/home/pi”, i recommend also to create a Subfolder for this scripts
- Set permissions on the files every file need the permission that owner can run it (i do this over the GUI, if you have a console only pi you have to do this via comandline)
- Get the service name type the command in your console on your Raspberry to get a List of your services:
service --status-all
- Create a new “ShellSwich” (here example with “z-way-server”-service)
{
"onCommand": "/home/pi/pimatic-scripts/service_start.sh z-way-server",
"offCommand": "/home/pi/pimatic-scripts/service_stop.sh z-way-server",
"getStateCommand": "/home/pi/pimatic-scripts/service_state.sh z-way-server",
"interval": 60,
"id": "YOURID",
"name": "YOURNAME",
"class": "ShellSwitch"
}
You can use as Interval what you want but i recommend that you not set it to low because its not necessary!
That´s it, the Switch will have automatically the state of the service and you can also start or stop the service by switching the Switch and you can setup rules that you get a Telegram Message if the service is stopped and start the service again.