Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StatelessProgrammableSwitch not triggering actions in Home App #73

Closed
gui28347 opened this issue Dec 27, 2020 · 6 comments
Closed

StatelessProgrammableSwitch not triggering actions in Home App #73

gui28347 opened this issue Dec 27, 2020 · 6 comments

Comments

@gui28347
Copy link

Not sure if I'm doing it correctly, after creating a Stateless button, I can see it in homebridge with no issues.

I used my phone to associate an action to SINGLE_PRESS action (turn a specific light on)

I'm executing the script:
State.js Set Button3 ProgrammableSwitchEvent 0

The scene is not being triggered. I tried using a different plugin (https://github.com/jnvaldenaire/homebridge-http-programmableswitch/blob/master/index.js) and it worked with no issue (same phone & same light).

Is the parameter or value I'm using incorrect ?

Thanks,
Gui

@ztalbot2000
Copy link
Owner

ztalbot2000 commented Dec 27, 2020 via email

@gui28347
Copy link
Author

  • Plugin installed: homebridge-cmd4 v2.4.2

  • My config.json :
    {
    "platform": "Cmd4",
    "name": "Cmd4",
    "outputConstants": false,
    "accessories": [
    {
    "type": "StatelessProgrammableSwitch",
    "displayName": "Button3",
    "programmableSwitchEvent": "SINGLE_PRESS",
    "name": "Button3",
    "serviceLabelIndex": 1,
    "Manufacturer": "Somebody",
    "Model": "Anything",
    "SerialNumber": "12345",
    "stateChangeResponseTime": 3,
    "state_cmd": "node .homebridge/Cmd4Scripts/State.js"
    }
    ]
    }

  • I can see the button in My iphone and homebridge UI.

  • Steps to repro:

  1. Open your home app, click the button, and assign an action to "SINGLE_PRESS", such as "turn on or turn off a light"
  2. Execute: ./State.js Set Button3 ProgrammableSwitchEvent 0
  3. At this point we expect that ./State.js would trigger an Event SINGLE_PRESS and the action configured in step Allow State.js to be run from anywhere #1 would happen, but nothing happens.

Let me know if you need any additional info.
Thanks for your help

Gui

@ztalbot2000
Copy link
Owner

Hi,

I certainly don't know every device and every characteristic. I wrote this plugin just to see what the Home App on IOS could do without any devices. It has evolved into this do everything for everyone plugin.
I used your config.json and tried it in CMD4 2.4.2. I was surprised to see no getCmd of anything in the debug logs. Specifically I was looking for:
getValue accTypeEnumIndex:(114)-'ServiceLabelIndex' function for:Button3 cmd:node .homebridge/Cmd4Scripts/State.js Get 'Button3' 'ServiceLabelIndex'
[12/28/2020, 3:51:26 PM] [Cmd4] getValue ServiceLabelIndex function for:Button3 returned:0

I achieved this by adding the following to your config.json
"state_cmd": "node .homebridge/Cmd4Scripts/State.js",
"polling": [
{"serviceLabelIndex": "1", "interval": 40, "timeout": 8000}
]
Now the State.js command I provide is not smart enough to increment the serviceLabelIndex as does the plugin you stated. I could add this. No biggee.
Give this a shot and let me know what happens. I think that is all I can do for today.

@gui28347
Copy link
Author

Thanks for quick reply!

I tried with your proposed change, so now I have in my config:

     {
        "platform": "Cmd4",
        "name": "Cmd4",
        "outputConstants": false,
        "accessories": [
            {
                "type": "StatelessProgrammableSwitch",
                "displayName": "Button3",
                "programmableSwitchEvent": "SINGLE_PRESS",
                "name": "Button3",
                "serviceLabelIndex": 1,
                "Manufacturer": "Somebody",
                "Model": "Anything",
                "SerialNumber": "12345",
                "stateChangeResponseTime": 3,
                "state_cmd": "node .homebridge/Cmd4Scripts/State.js",
                "polling": [
                    {
                        "serviceLabelIndex": "1",
                        "interval": 40,
                        "timeout": 8000
                    }
                ]
            }
        ]
    }
  • After changing my config, I restarted homebridge.
  • Open my phone app, un-assigned all actions from button3
  • Configured only 1 action in SINGLE_PRESS (turn on kitchen light)
  • Executed State.js Set Button3 ProgrammableSwitchEvent 0
  • Waited 1 minute to validate if it takes some time to read the "buttonPress" event, but nothing happens even after 1 minute.

Thanks
Gui

@gui28347
Copy link
Author

I believe I found, not sure yet where add it to your code to fix the issue.

I found in the other plugin, they are using " self.triggerSwitch(i, 1); " to cause the trigger event to homebridge. I believe you are not using it.

@gui28347
Copy link
Author

Couple changes and I got the button working.

#1 - Add this function to index.js

function readFSLastWrittenTime(a,b)
{
var fn = "/home/homebridge/.homebridge/Cmd4Scripts/Cmd4States/Status_" + a + "_" + b;
var stats = fs.statSync(fn);
let seconds = (new Date().getTime() - stats.mtime) / 1000;
return seconds;
}

#2 - Change the getvalue:function

if (characteristicString == "ServiceLabelIndex")
{
var results = readFSData(this.displayName ,characteristicString);

      var LastWrittenSec = readFSLastWrittenTime(this.displayName ,characteristicString);
     if (LastWrittenSec < 3)
     {
        var char = self.service.getCharacteristic(Characteristic.ProgrammableSwitchEvent);
        char.setValue(results);
     }
  }
  else
  {
      var results = readFSData(this.displayName ,characteristicString);
  }

#3 - Set polling to 3 seconds in your config file for button. This way it will monitor and if file modified date is less than 3 seconds it will trigger the action which the value of file contents.

Feel free to re-use this code and add to your project if you want, or just close this issue.

BTW: I moved read/write function to index.js so no external child process being created. You can still use State.js to read/write files, but for people using polling in multiple accessories or with high frequency polling the performance is much better without having to create external processes.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants