luisiam / homebridge-cmdswitch2

CMD Plugin for HomeBridge (API 2.0): https://github.com/nfarina/homebridge
Apache License 2.0
176 stars 29 forks source link

Always on #16

Open berkaytheunicorn opened 7 years ago

berkaytheunicorn commented 7 years ago

Hey there, I have simple set up


"switches" : [
        {
          "state_cmd" : "gpio read 4", // returns 1 or 0 also try to create a bash script to return true or false
          "name" : "Lamp",
          "on_cmd" : "gpio write 4 1",
          "off_cmd" : "gpio write 4 0",
      "pooling": true
        }
]

with bash


"switches" : [
        {
          "state_cmd" : "/home/pi/read.sh",
          "name" : "Lamp",
          "on_cmd" : "gpio write 4 1",
          "off_cmd" : "gpio write 4 0",
      "pooling": true
        }
]

bash

#!/bin/sh

# GPIO setup:
# Pin 4 (GPIO0): switch 

if [ "`gpio read 4`" -eq 1 ]; then
    echo true
fi;

if [ "`gpio read 4`" -eq 0 ]; then
    echo false
fi;

on off works great but after I off the lamp 1-2 secons later it just became on in state but not really.

am i doing something wrong here?

luisiam commented 7 years ago

The state checking algorithm is basically checking if the state command returns an empty string. You can try gpio read 4 | grep -i "true" something like that.

theend92 commented 6 years ago

Try this:

"switches" : [
        {
          "state_cmd" : "sh /home/pi/read.sh",
          "name" : "Lamp",
          "on_cmd" : "gpio write 4 1",
          "off_cmd" : "gpio write 4 0",
      "pooling": true
        }
]

read.sh

gpioread = $(gpio read 4)
if [ $gpioread -eq 1 ]
then
exit 0
fi

if [ $gpioread -eq 0 ]
then
exit 1
fi

HomeKit need a Exit-Code 0 for device ON and all other Exit-Codes = OFF. "gpio read" gives a "1" or a "0". But dont gives a Error-Code "1" or "0". "gpio read" gives always Error-Code 0, and that is the code for ON.