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

iOS HomeKit API - Homebridge #193

Open
pihome-shc opened this issue Mar 30, 2020 · 147 comments
Open

iOS HomeKit API - Homebridge #193

pihome-shc opened this issue Mar 30, 2020 · 147 comments

Comments

@pihome-shc
Copy link
Owner

Homebridge is a lightweight NodeJS server that emulates the iOS HomeKit API. This would be good add-on for PiHome if we can get this one working with PiHome.

Help and Reference:

@dvdcut
Copy link
Contributor

dvdcut commented Apr 2, 2020

@pihome-shc i have installed Homebridge with webgui now what is next? is there any process to read temperature or show temperature on apple devices?

@twa127
Copy link
Contributor

twa127 commented Apr 2, 2020

I'm at the same point, I guess what we need to decide is what we want to achieve eg do the same as with Echo or more?

I installed https://www.npmjs.com/package/homebridge-dummy plugin which at least demonstrates a dummy switch and this one could possibly a basis for talking to pihome server https://github.com/Supereg/homebridge-http-switch

@dvdcut
Copy link
Contributor

dvdcut commented Apr 2, 2020

@twa127 question on echo: can we add another option to read temperature ? like alexa what is the temperature for (zone name) ?

i think all these compatibility device should have same options. i mean from apple home kit we can not control schedule or anything but boost only ? display temperature with zone name ? or you are thinking adding more options? i m still trying to understand how Homebridge works and youtube only talks about all commercial solutions and add them but nothing about custom solution or adding your own stuff.

@twa127
Copy link
Contributor

twa127 commented Apr 2, 2020

With Echo we are emulating a WeMo switch which only responds to ON and OFF and returns a boolean reflecting the state of the switch. For temperature we would need something else but I guess that must be possible.

Custom solutions are possible like the dummy switch, just need you to construct the index.js and package.json code, the functionality is in index.js

@twa127
Copy link
Contributor

twa127 commented Apr 2, 2020

It would not take much to turn the dummy switch code in to a Boost control, node.js has a mysql module so we could update the boost status value depending if the switch were ON or OFF

@pihome-shc
Copy link
Owner Author

Adding to read temperature for any zone would be good addition on echo but so far i m very happy with the work @twa127 have done.
as far as Homebridge goes it would be zone icons with zone name and if tape on zone button it turn on boost for that zone. i dont think more can be achieved and i dont own any commercial solution to experiment with or what commercial or non commercial solutions can do in homekit.

my knowledge is very limited on python and next to nothing on node.js but may be its time to upskill myself.

ps: this lockdown is killing me getting home sick now :(

@twa127
Copy link
Contributor

twa127 commented Apr 3, 2020 via email

@dvdcut
Copy link
Contributor

dvdcut commented Apr 3, 2020

@twa127 can i help in anyway?

@twa127
Copy link
Contributor

twa127 commented Apr 3, 2020

thanks for the offer, I'll let you know when i get my head around it all :-)

@twa127
Copy link
Contributor

twa127 commented Apr 4, 2020

I've got something working for me using the HTTP Switch example and a bit of python acting as the response server. Still a work in progress :-))

  1. sudo pip install flask request
  2. sudo npm install -g homebridge-http-switch
  3. edit /var/lib/homebridge/config.json and replace the accessories section with the one in the attached zip, you will need to change the zone names in the urls to match your config. You can add extra switches as required, remember the comma between each accessory section
  4. run the response server with 'python homekit_api.py' this can run as a service later after testing

homekit_api.zip

@pihome-shc
Copy link
Owner Author

@twa127 that is fantastic work.

@pihome-shc
Copy link
Owner Author

@twa127, great work, good thinking to run python script as service also can we add this under setting->services, any option we can auto generate accessories list from all exiting zone,

@twa127
Copy link
Contributor

twa127 commented Apr 6, 2020

I had the same thought about something to add the accessories based on the zones, I'll work on that, perhaps make it part of the install shell.

Comitted the change to add the API to the services list

@pihome-shc
Copy link
Owner Author

@twa127
here are results of new install script, but if i manually run python config_json.py then it updates accessories.


root@pihome:/var/www/homekit# systemctl stop pihome_homekit_api.service
root@pihome:/var/www/homekit# sh ./install_homekit.sh
Checking if python-requests is Installed
python-requests already installed
Checking if python-flask is Installed
python-flask already installed
Checking For Existing Unit File
Deleting Existing Unit File: /lib/systemd/system/pihome_homekit_api.service
Creating Unit File: /lib/systemd/system/pihome_homekit_api.service
Enabling the service
Starting the service
./install_homekit.sh: 70: ./install_homekit.sh: Syntax error: Unterminated quoted string

@pihome-shc
Copy link
Owner Author

@twa127 at line 67 missing "

@twa127
Copy link
Contributor

twa127 commented Apr 6, 2020 via email

@twa127
Copy link
Contributor

twa127 commented Apr 7, 2020

Hi,

I've added getTemperature to the homekit api so that you can get the temperature for any zone and use in hombridge. You need to install the homebridge-http-temperature-sensor plugin and add a new accessory to the homebridge config eg

    {
        "accessory": "HTTP-TEMPERATURE",
        "name": "Ground Floor",
        "pullInterval": "1000",
        "getUrl": {
            "url": "http://localhost:8081/api/getTemperature?zonename=Ground Floor",
            "method": "GET"
        }
    },

Choose your zone name and use in the url, you can add for as many zones as you like and then setup rooms on your iOS device

@pihome-shc
Copy link
Owner Author

@twa127
after updating and installing, i have two icons for each zone and its not showing temperature for any zone.

@twa127
Copy link
Contributor

twa127 commented Apr 8, 2020 via email

@pihome-shc
Copy link
Owner Author

@twa127 here is my accessories config.

    "accessories": [
        {
            "name": "Ground Floor",
            "pullInterval": "1000",
            "accessory": "HTTP-SWITCH",
            "statusUrl": {
                "url": "http://localhost:8081/api/switchStatus?zonename=Ground Floor",
                "method": "GET"
            },
            "switchType": "stateful",
            "onUrl": {
                "url": "http://localhost:8081/api/switchOn?zonename=Ground Floor",
                "method": "GET"
            },
            "offUrl": {
                "url": "http://localhost:8081/api/switchOff?zonename=Ground Floor",
                "method": "GET"
            }
        },
        {
            "name": "First Floor",
            "pullInterval": "1000",
            "accessory": "HTTP-SWITCH",
            "statusUrl": {
                "url": "http://localhost:8081/api/switchStatus?zonename=First Floor",
                "method": "GET"
            },
            "switchType": "stateful",
            "onUrl": {
                "url": "http://localhost:8081/api/switchOn?zonename=First Floor",
                "method": "GET"
            },
            "offUrl": {
                "url": "http://localhost:8081/api/switchOff?zonename=First Floor",
                "method": "GET"
            }
        },
        {
            "name": "Hot Water",
            "pullInterval": "1000",
            "accessory": "HTTP-SWITCH",
            "statusUrl": {
                "url": "http://localhost:8081/api/switchStatus?zonename=Hot Water",
                "method": "GET"
            },
            "switchType": "stateful",
            "onUrl": {
                "url": "http://localhost:8081/api/switchOn?zonename=Hot Water",
                "method": "GET"
            },
            "offUrl": {
                "url": "http://localhost:8081/api/switchOff?zonename=Hot Water",
                "method": "GET"
            }
        },
        {
            "accessory": "HTTP-TEMPERATURE",
            "name": "Ground Floor",
            "pullInterval": "1000",
            "getUrl": {
                "url": "http://localhost:8081/api/getTemperature?zonename=Ground Floor",
                "method": "GET"
            }
        },
        {
            "accessory": "HTTP-TEMPERATURE",
            "name": "First Floor",
            "pullInterval": "1000",
            "getUrl": {
                "url": "http://localhost:8081/api/getTemperature?zonename=First Floor",
                "method": "GET"
            }
        },
        {
            "accessory": "HTTP-TEMPERATURE",
            "name": "Hot Water",
            "pullInterval": "1000",
            "getUrl": {
                "url": "http://localhost:8081/api/getTemperature?zonename=Hot Water",
                "method": "GET"
            }
        }
    ],

@twa127
Copy link
Contributor

twa127 commented Apr 8, 2020

Looks okay to me, the only thing I'm doing different is that I've got the temperature sensors all before the switches.

@twa127
Copy link
Contributor

twa127 commented Apr 8, 2020

Mine-

"accessories": [
    {
        "accessory": "HTTP-TEMPERATURE",
        "name": "Ground Floor",
        "pullInterval": "1000",
        "getUrl": {
            "url": "http://localhost:8081/api/getTemperature?zonename=Ground Floor",
            "method": "GET"
        }
    },
    {
        "accessory": "HTTP-TEMPERATURE",
        "name": "Lounge",
        "pullInterval": "1000",
        "getUrl": {
            "url": "http://localhost:8081/api/getTemperature?zonename=Lounge",
            "method": "GET"
        }
    },
    {
        "accessory": "HTTP-SWITCH",
        "name": "Hot Water",
        "switchType": "stateful",
        "pullInterval": "1000",
        "onUrl": {
            "url": "http://localhost:8081/api/switchOn?zonename=Ch. Hot Water",
            "method": "GET"
        },
        "offUrl": {
            "url": "http://localhost:8081/api/switchOff?zonename=Ch. Hot Water",
            "method": "GET"
        },
        "statusUrl": {
            "url": "http://localhost:8081/api/switchStatus?zonename=Ch. Hot Water",
            "method": "GET"
        }
    },
    {
        "accessory": "HTTP-SWITCH",
        "name": "Central Heating",
        "switchType": "stateful",
        "pullInterval": "1000",
        "onUrl": {
            "url": "http://localhost:8081/api/switchOn?zonename=Ground Floor",
            "method": "GET"
        },
        "offUrl": {
            "url": "http://localhost:8081/api/switchOff?zonename=Ground Floor",
            "method": "GET"
        },
        "statusUrl": {
            "url": "http://localhost:8081/api/switchStatus?zonename=Ground Floor",
            "method": "GET"
        }
    }
],

@dvdcut
Copy link
Contributor

dvdcut commented Apr 28, 2020

saw this on pihome website.
http://www.pihome.eu/2020/04/28/pihome-with-ios-homekit/
@twa127 you deserve and thank you 👍

@twa127
Copy link
Contributor

twa127 commented Apr 28, 2020 via email

@JSa1987
Copy link
Contributor

JSa1987 commented May 10, 2020

Hi all,

I followed the instructions at http://www.pihome.eu/2020/04/28/pihome-with-ios-homekit/.
The thing that I had to do differently is that I had to install nodes v. 11.15.0 as I'm running this on a Raspberry Pi Zero (armv6l). I downloaded this from bhttps://nodejs.org/dist/v11.15.0/.

The switches are working great. While the temperature gauges show 0 degC for all zone. Any advice on what I could try to fix this?

Thank you.

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

have a look in /var/lib/homebridge/.node-persist/storage
you should see a file for each switch and sensor, the sensor file contents should look like this -
{"key":"http-webhook-sensor38","value":"23.00"}

Also does the API return anything eg
http://192.168.0.100/api/getTemperature?zonename=Lounge (your PiHome ip and zonename)
gives something like {"success":true,"state":"23.10"}

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

The zone states and sensors are updated by boiler.php which is running as a cron job. boiler.php looks to see if the webhooks platform is installed and then does the update if required. sensors and switches are identified by sensorxx or switchxx wher xx is the id for the zone in the zone table

hope that helps

@JSa1987
Copy link
Contributor

JSa1987 commented May 10, 2020

When I run the API I can get the correct temperature for the different zones. For example If I got to http://192.168.1.2/api/getTemperature?zonename=Living%20Room I get {"success":true,"state":"18.30"}

In the /var/lib/homebridge/.node-persist/storage I have only one file called 19afcbe0f11a2084b6d026d1002341e9, the content of this file is {"key":"http-webhook-switch39","value":false}.

In the homebridge log file I have:

[5/10/2020, 7:54:13 PM] [HttpWebHooks] Getting current state for 'sensor39'...
[5/10/2020, 7:54:13 PM] [HttpWebHooks] Getting current state for 'sensor39'...
[5/10/2020, 7:54:13 PM] [HttpWebHooks] State for 'sensor39' is 'undefined'
[5/10/2020, 7:54:13 PM] [HttpWebHooks] State for 'sensor39' is 'undefined'
[5/10/2020, 7:54:13 PM] [HttpWebHooks] Getting current state for 'sensor40'...
[5/10/2020, 7:54:13 PM] [HttpWebHooks] Getting current state for 'sensor40'...
[5/10/2020, 7:54:13 PM] [HttpWebHooks] State for 'sensor40' is 'undefined'
[5/10/2020, 7:54:13 PM] [HttpWebHooks] State for 'sensor40' is 'undefined'
[5/10/2020, 7:54:13 PM] [HttpWebHooks] Getting current state for 'sensor38'...
[5/10/2020, 7:54:13 PM] [HttpWebHooks] Getting current state for 'sensor38'...
[5/10/2020, 7:54:13 PM] [HttpWebHooks] State for 'sensor38' is 'undefined'
[5/10/2020, 7:54:13 PM] [HttpWebHooks] State for 'sensor38' is 'undefined'

The switches are working well, they trigger the boost as expected.

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

Have you got 'graph enabled for those zones?

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

also do those node_ids show up in the view 'messages_in_view_24h'

@JSa1987
Copy link
Contributor

JSa1987 commented May 10, 2020

Hi twa127,

Yes, the graph option is enabled for the zones and the temperature shows up in the message_in_view24h.

I was checking the /var/lib/homebridge/config.json file and notice that this is what I have for the sensors:

"sensors": [
                {
                    "id": "sensor38", 
                    "name": "Bedroom Temperature", 
                    "type": "temperature"
                }, 
                {
                    "id": "sensor39", 
                    "name": "Living Room Temperature", 
                    "type": "temperature"
                }, 
                {
                    "id": "sensor40", 
                    "name": "Bathroom Temperature", 
                    "type": "temperature"
                }, 
               {
                    "id": "sensor41", 
                    "name": "Hot Water Temperature", 
                    "type": "temperature"
                }
            ]

Should there be some line similar to the following ones that I have for the switches?

                    "on_url": "http://127.0.0.1/api/boostSet?zonename=Bedroom&s$
                    "on_method": "GET", 
                    "off_url": "http://127.0.0.1/api/boostSet?zonename=Bedroom&$
                    "off_method": "GET"

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

nope the temperatures are reported by boiler.php

please try adding the following at line 97 in boiler.php

print_r($sensors);

then run boiler.php with php /var/cron/boiler.php you should see something like this at the top of the output

Array
(
[0] => 33
[1] => 38
)

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

the assumptions are made that the webhooks directory is /usr/lib/node_modules/homebridge-http-webhooks

and the path to the homebridge config is /var/lib/homebridge/config.json

and that homebridge is running as a service

@JSa1987
Copy link
Contributor

JSa1987 commented May 10, 2020

Hi twa127,

Below is the output of running boiler.php after adding print_r($sensors); on line 97:

2020-05-10 20:41:36 - Boiler Script Started 
Array
(
)
---------------------------------------------------------------------------------------- 
2020-05-10 20:41:36 - Day of the Week: 0 
2020-05-10 20:41:36 - Zone: Sensor Reading     16.10 
2020-05-10 20:41:36 - Zone: Weather Factor     0.3 
2020-05-10 20:41:36 - Zone: DeadBand           0.5 
2020-05-10 20:41:36 - Zone: Temperature        16.9 
2020-05-10 20:41:36 - Zone ID: 38 
2020-05-10 20:41:36 - Zone: Bedroom Controller: 101 Controller Child: 3 Zone Status: 1 
2020-05-10 20:41:36 - Zone: Bedroom Start Cause: Schedule Started - Target C:18 Zone C:16.4 
---------------------------------------------------------------------------------------- 
2020-05-10 20:41:36 - Zone: Sensor Reading     18.00 
2020-05-10 20:41:36 - Zone: Weather Factor     0.3 
2020-05-10 20:41:36 - Zone: DeadBand           0.3 
2020-05-10 20:41:36 - Zone: Temperature        18.6 
2020-05-10 20:41:36 - Zone ID: 39 
2020-05-10 20:41:36 - Zone: Living Room Controller: 101 Controller Child: 1 Zone Status: 0 
2020-05-10 20:41:36 - Zone: Living Room Stop Cause: No Schedule - Target C:15 Zone C:18.3 
---------------------------------------------------------------------------------------- 
2020-05-10 20:41:36 - Zone: Sensor Reading     18.10 
2020-05-10 20:41:36 - Zone: Weather Factor     0.3 
2020-05-10 20:41:36 - Zone: DeadBand           0.5 
2020-05-10 20:41:36 - Zone: Temperature        18.9 
2020-05-10 20:41:36 - Temperature sensor communication timeout for This Zone. Last temperature reading: 2020-05-10 17:56:55
2020-05-10 20:41:36 - Zone ID: 40 
2020-05-10 20:41:36 - Zone: Bathroom Controller: 101 Controller Child: 2 Zone Status: 0 
2020-05-10 20:41:36 - Zone: Bathroom Stop Cause: Zone fault - Target C:15 Zone C:18.4 
---------------------------------------------------------------------------------------- 
2020-05-10 20:41:36 - Zone: Sensor Reading     18.50 
2020-05-10 20:41:36 - Zone: Weather Factor     0.3 
2020-05-10 20:41:36 - Zone: DeadBand           5 
2020-05-10 20:41:36 - Zone: Temperature        23.8 
2020-05-10 20:41:36 - Zone ID: 41 
2020-05-10 20:41:36 - Zone: Hot Water Controller: 101 Controller Child: 4 Zone Status: 0 
2020-05-10 20:41:36 - Zone: Hot Water Stop Cause: No Schedule - Target C:15 Zone C:18.8 
---------------------------------------------------------------------------------------- 
2020-05-10 20:41:36 - Boiler Expected End Time: 2020-05-10 21:30:00
2020-05-10 20:41:36 - Boiler Node ID: 101 Child ID: 0 
2020-05-10 20:41:36 - Boiler Fired Status: 1 
2020-05-10 20:41:36 - Boiler Hysteresis Status: 0 
---------------------------------------------------------------------------------------- 
2020-05-10 20:41:36 - Boiler Script Ended 
**************************************************************************************** 

Yes, config.json is located in /var/lib/homebridge/

I do not have the /usr/lib/node_modules/ folder...

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

okay first you need to find where the webhooks directory is located ie
cd /
find / -name homebridge-http-webhooks

then edit boiler.php and change line 62 -
$path = '/usr/lib/node_modules/homebridge-http-webhooks'; // path to webhooks directory

using the path you located with the find

@JSa1987
Copy link
Contributor

JSa1987 commented May 10, 2020

That fixed it!

My webhooks folder is:
/usr/local/lib/node_modules/homebridge-http-webhooks

Thank you!

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

great I'll change boiler.php to find the file paths

@pihome-shc
Copy link
Owner Author

@twa127 great as always. do we need to amend documentation to include some troubleshooting steps/commands?

@twa127
Copy link
Contributor

twa127 commented May 10, 2020

I'll amend boiler.php to find the path dynamically, so no need for doc change

@pihome-shc
Copy link
Owner Author

@twa127 what if someone do not have homebridge installed on their system?

@twa127
Copy link
Contributor

twa127 commented May 11, 2020 via email

@twa127
Copy link
Contributor

twa127 commented May 11, 2020

Now fixed for systems where homebridge not installed

@scottagecheeseandcrackers

@twa127 - fantastic work on the homebridge implementation. I just updated to the latest release and have just tested homebridge - no issues - all aspects running great. All zones & Boost switches showing in Homebridge without issue.

I have installed the "homebridge-gsh" google home plugin and have tested this via the google home app. Unfortunately the homebridge google home plugin does not support temp sensors yet - so only my boost switches have appeared.

If it helps - I have managed to set up thermostats in google home via openhab as a means to query the pihome database. As the process is similar it may be of use here. To add a thermostat in google home it must have min 3 components:

(Mandatory) Mode: Number or String (off, heat, cool, on, ...). { ga="thermostatMode" }
(Mandatory) Temperature Ambient: Number. { ga="thermostatTemperatureAmbient" }
(Mandatory) Temperature Setpoint: Number. { ga="thermostatTemperatureSetpoint" }

On my own system I have set up thermostat mode as 'heat' and temperaturesetpoint 'as my cut-off temp'. Once I have specified these values the temp for that zone appears on my google home as a simple thermostat. I can query current temperature - and it spits it back out (fyi - this value is rounded off to the nearest .5'c). Really I am only using this setup to allow me to query individual room temperatures by voice. However it could be utilized to adjust the boost cut off temp and turn on/off boost.

If there was a simple way to map the current 'cutofftemp' in pihome as the 'thermostatTemperatureSetpoint' and to map the current 'boost status' (off/heat) in pihome as the 'thermostatMode' then I think the google home integration would work.

Hope this helps. Awesome work on the updates guys.

@twa127
Copy link
Contributor

twa127 commented May 26, 2020

Hi,

I'll try and get my head around this :-)

@scottagecheeseandcrackers

FYI - the Google smart home - homebridge integration was updated earlier today. The latest release now supports temperature sensors. Link to latest release - [https://github.com/oznu/homebridge-gsh/releases/tag/v2.0.0] .

I've just updated mine - and I can now ask google what temperature each zone is. Thanks again @twa127 for the hard work.

@pihome-shc
Copy link
Owner Author

@scottagecheeseandcrackers thank you for the update.

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

No branches or pull requests

5 participants