oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

MagPi 76

Fri Nov 30, 2018 12:23 pm

If there was nothing else interesting in any other edition of MagPi, I would still pay for them all just for the article about Ikea lights!

I've got loads of their bulbs around the house as I'm swapping all my lights from Hive to Tradfri. One of the things missing from the Tradfri platform is the ability to turn a light on at sunset - you can only set an absolute time.

So, this morning, I thought I'd have a play (I'm very new to Python so this isn't intuitive). Couple of hours later and I've got a little Python script that gets the sunset time, compares it with the current time and switches the lights on if it's later than sunset. I'm running some other home automation stuff with a script that runs every few minutes so it'll be dead easy to add this in.

For the record, as it's in the spirit of "hacking" home automation, my pet project, which is what got me started in this, is occupancy detection so that I can control the heating depending on who's in. I've now got that 99% working. There's a Pi in the lounge that sniffs for the Bluetooth MAC addresses of the phones in the house. If it finds one, it increments a score. If the score is zero then it turns the heating down. I've even got it weighted so that if I'm in the house it turns the heating up a bit more. I'm using the very badly documented Hive API to do this so it's been a bit of a labour of love. When I started this, I knew nothing about Python or APIs, or much else that would help with this. I don't do "Hello World!" so this has definitely been a bit of a baptism of fire for me.

This is the rough way that it works:

Image

I've done a bit of a write up here:

http://2610.co.uk/blog/hivehome-geofencing/

Lots more to do on it. For starters, I can now add in the functionality on the dashboard to turn my Ikea lights on and off!

Andyroo
Posts: 664
Joined: Sat Jun 16, 2018 12:49 am

Re: MagPi 76

Fri Nov 30, 2018 2:28 pm

Please post an update on this when you have it testing / running - I've been banned from Ikea trips for the rest of this year (OK we did get so much last time I had to run my wife home and come back for the rest - 2+ hour round trip) so I'm interested in seeing how this works.

I've read very mixed reviews of the Ikea 'hub' - some folk have no issues while others need to power cycle often as it drops off the network.
Need Pi spray - these things are breeding in my house...

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Fri Nov 30, 2018 2:36 pm

Yeah, we went over to the Reading store for a couple of Kilner jars and I came out with 95 quid's worth of bulbs.

I really like the Ikea stuff. It's cheap as chips and (touch wood) we've not had a problem with any of it. The dimmer switch things are rubbish but we tend to use Alexa for everything anyway.

The sunset thing WILL work. I've got the script running and I just reversed it so that it checked to see if the time was less than sunset and switched the lights on.

The bit that took a while was getting it to run on a cron job. Because the code in the mag pulls in the config, I was getting a permission error. I tried running the task as root, as pi, under pi's own cron... everything I could think of. in the end, I took the values it was pulling from the config file and hardcoded them into my Python script. That worked.

So, sunset is 4.02pm. I've got an alarm set for 4.00 and my script runs once a minute so another hour and a half and I will know if it works in real life. So exciting!

Andyroo
Posts: 664
Joined: Sat Jun 16, 2018 12:49 am

Re: MagPi 76

Fri Nov 30, 2018 3:14 pm

Your post had me in stitches.
oldjake wrote:
Fri Nov 30, 2018 2:36 pm
...
So, sunset is 4.02pm. I've got an alarm set for 4.00 and my script runs once a minute so another hour and a half and I will know if it works in real life. So exciting!
I hope it does not get dark early for you - you may not be able to see the clock to check if it worked :lol:

Seriously though - any plans to add a light sensor to override the 'sunset' time to cope for rainy / foggy / snowy days?
Need Pi spray - these things are breeding in my house...

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Fri Nov 30, 2018 4:37 pm

I don't know about the light sensor. It might be difficult to calibrate. We've got 5 lamps in the lounge that are supposed to all come on at sunset. 4 of them are Hive at the moment and they come on a few seconds apart.

Andyroo
Posts: 664
Joined: Sat Jun 16, 2018 12:49 am

Re: MagPi 76

Fri Nov 30, 2018 4:48 pm

I've visions of Christmas lights flashing or planes landing now!

When dear old computers can work out 'its about dark enough' then we have IoT cracked :lol:

I'm planning on a little test for a while over winter based on a light sensor and a button - I'll push the button when I feel I need lights on (assuming they are off) and see how close they are together. I will be happy with a little difference I think and I can always get off my lazy (feel free to fill the next word in) and turn them on manually.
Need Pi spray - these things are breeding in my house...

User avatar
Paul Webster
Posts: 762
Joined: Sat Jul 30, 2011 4:49 am
Location: London, UK

Re: MagPi 76

Fri Nov 30, 2018 6:26 pm

The nearest Ikea to me has been selling Christmas trees for the last couple of years.
25 quid for the tree and you get a 25 quid voucher to spend in January ...
Tradfri is on my list this year to experiment with (I have a Philips and Lifx bulbs in the house - controlled by various things including HASS).
Also recently bought some "Twinkly" WiFi controllable LEDs to go on the tree ...

Andyroo
Posts: 664
Joined: Sat Jun 16, 2018 12:49 am

Re: MagPi 76

Fri Nov 30, 2018 9:43 pm

Sorry - forgot to say I like the blog and diagram :)
Need Pi spray - these things are breeding in my house...

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Sat Dec 01, 2018 8:43 am

Thanks. I think it does the job of explaining how it all works.

On the subject of the sunset thing... It didn't work :( It was fine before sunset as the comparison was always returning false. But it still returned false when sunset arrived. I've changed my code so fingers crossed for about 4.00!

User avatar
bensimmo
Posts: 3458
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: MagPi 76

Sat Dec 01, 2018 10:43 am

For reference, as I'm reading through your blog post, My Bosch/Worcester/Nefity Wave setup used GPS for geofencing (hence the geo part).
But it means everyone needs to have a phone, have a mobile connection and have it on and well I have kids so that's not going to happen.
The app then tells the knows if a person is inside or outside that area and send it to the unit to modulate the boiler.
Nice idea, but not everyone has a phone on them.

User avatar
bensimmo
Posts: 3458
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: MagPi 76

Sat Dec 01, 2018 10:49 am

Oh and spin a projects thread out in the projects section. Get some code up (as I'm nosey ;))
then get an article in MagPi :-)

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Sat Dec 01, 2018 1:19 pm

bensimmo wrote:
Sat Dec 01, 2018 10:43 am
For reference, as I'm reading through your blog post, My Bosch/Worcester/Nefity Wave setup used GPS for geofencing (hence the geo part).
But it means everyone needs to have a phone, have a mobile connection and have it on and well I have kids so that's not going to happen.
The app then tells the knows if a person is inside or outside that area and send it to the unit to modulate the boiler.
Nice idea, but not everyone has a phone on them.
I'm looking at alternatives to having to have a phone. I bought one of those Tile things. I can detect it from the Pi using Bluepy but unfortunately it's not permanently acting as a beacon. I think that's how they get the battery life. So it probably advertises itself every 30 seconds or something. So if you lose your keys and use the app then waiting 30 seconds for it to pick them up isn't a massive problem. But if you have a Python script that sniffs every 60 seconds and gives each device 10 seconds to respond then it's going to be pot luck as to whether your sniff coincides with the Tile broadcasting. If I had more time, I'd see if I could work out what the pattern was and set the timeout so that it would always overlap with a broadcast from the Tile. The problem there is that I'd have to compromise on the lag with the occupancy. I'm checking every 60 seconds at the moment. I suppose it wouldn't be a great deal if I had to push that to 5 minutes so that the Tile detection had a chance.

Hmmm... might have to do a bit more playing with that.

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Sat Dec 01, 2018 1:52 pm

I should probably start a new thread for this.

I've been back and had a look at this Tile thing. As I was playing, I noticed that it also picks up my Fossil hybrid watch... bonus!

Anyway, whichever of these I scan for, I get similar results. This is my code:

Code: Select all

from bluepy.btle import Scanner, DefaultDelegate
import bluetooth
import time
import datetime

class ScanDelegate(DefaultDelegate):
        def __init__(self):
                DefaultDelegate.__init__(self)
        def handleDiscovery(self, dev, isNewDev, isNewData):
                if isNewDev:
                        ts = time.time()
                        timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
                        score = 0
                        if "cc:1c:41:40:ec:11" in dev.addr:
                        #if "c1:ed:da:df:55:46" in dev.addr:
                                print "Andy in", timestamp
                        else:
                                print "Andy out", timestamp
                elif isNewData:
                        print "Received new data from", dev.addr
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(15.0)
I ran this 10 times and this is the sort of result that comes through:

Code: Select all

Andy out 2018-12-01 13:38:26
Andy out 2018-12-01 13:38:28
Andy in 2018-12-01 13:38:31
15 seconds timeout for the scan always comes back with 3 results and it's always 2 x out and 1 x in. Not always in the same order and the time between varies from 1 second to 11 seconds.

So, if I ran the scan every 5 minutes, say, and incremented the score for each result. If the score > 1 for any result set then I'm on, otherwise, I'm out. It's a bit of a kludge but it seems legit.

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Sat Dec 01, 2018 4:40 pm

:D

http://2610.co.uk/blog/more-home-automa ... ghts-hack/

If you have the sound on, the "Oh" is where I see the cron job fire, immediately followed by the light switching on

(This is in the context of the Ikea lights hack, by the way)

User avatar
mooblie
Posts: 105
Joined: Fri Oct 14, 2016 2:07 pm
Location: The Scottish Highlands

Re: MagPi 76

Sat Dec 01, 2018 5:13 pm

oldjake wrote:
Sat Dec 01, 2018 1:52 pm
I should probably start a new thread for this.....
Oldjake: Perhaps you could use a meaningful thread title for this and any future threads? It helps others browsing/lurking to decide if they are going to read or answer a thread, and "MagPi 76" is not helpful.

oldjake
Posts: 69
Joined: Fri Aug 03, 2018 12:39 pm

Re: MagPi 76

Sat Dec 01, 2018 5:22 pm

Yeah, sorry. I usually do. This started as just a nod to the article in issue 76 and grew arms and legs. Glad the forum police are on the case ;)

User avatar
bensimmo
Posts: 3458
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: MagPi 76

Sat Dec 01, 2018 8:29 pm

He/She has obviously not read many long threads on here, they often wander off into the distance.

I keep avoiding the Ikea lights as I know they are harmful to my time :lol: I might have to succumb though.

robthez
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 69
Joined: Thu Oct 11, 2012 10:50 am
Location: Bournemouth

Re: MagPi 76

Mon Dec 03, 2018 11:33 am

Nice!

Return to “The MagPi”