What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

kallefornia

New Around Here
This is a relatively simple bash script that turns off all lights when all members of a household have left and turns them back on again when one of them comes back.

Basically, it continually checks the list of clients currently connected to the router's WiFi network(s) for pre-defined IP addresses (e.g. of users' smartphones) and sends a command to the Hue bridge (via its API) to turn off the lights if it can't find any of them. It also remembers the last state of those lights and turns them back on when any of the pre-defined IP addresses reconnect.

I originally created this script because my brother keeps forgetting to turn off the lights when he leaves the house and it has since saved me a lot of unnecessary discussions and high utility bills, so I thought I'd share this, in case someone else might find it useful :)

You can find all the details and the files here: https://github.com/duckwilliam/hueoff

##############​

Description​

Bash script for automatically turning off the lights when all predefined IP-addresses have left WiFi on ASUSWRT Merlin and turn them back on when they reconnect.

Prerequisites:​

  • jq
  • curl
  • bash
  • jffs userscripts enabled
To find out how to get your Hue API hash see https://developers.meethue.com/ For this to work you mast have static DHCP enabled for the IP addresses you want to use

INSTALLATION:​

  • Configure this scripty by replacing the variables under User variables,
  • Make sure you don't use a randomised MAC address on your device when connecting to your WiFi network.
  • Make script executable (chmod +x ./hueoff) and run "hueoff install" from CLI OR:
  • Place this script in /jffs/scripts/
  • create this folder: /jffs/scripts//hueoff.d/logs
  • add the following lines to /jffs/scripts/post-mount:
/jffs/scripts/hueoff &> /jffs/scripts/hueoff.d/logs/log.txt &
  • To see the log run:
cat /jffs/scripts/hueoff.d/logs/log.txt"

Credits:​

Hue control functions were adapted from Harald van der Laan's Philips Hue Bash script: https://github.com/hvanderlaan/philips-hue

kallefornia 2023 https://github.com/duckwilliam/hueoff
 
Interesting way to detect presence! How well does this work?

(How often did one of you sit in the dark at home because of this?)
 
Interesting set up.

You might want to take a look at Home Assistant. It has a bunch of stuff for just this and can support interactive prompts to your phone too.

I used a similar approach, as we moved from full wfh to mixed/hybrid, to spot when none of us were in the house and send an actionable notification (as HA calls them) to my phone I can respond to to turn the heating off for the day. (The prompt was vital - I'd be in a lot of trouble if it accidentally turned the heating off when my wife was at home!)

HA can also work with definable zones and GPS if you have the companion app installed on phones though polling can make it tricky for rapid transitions - a friend wanted to use it to automatically open his garage door when he got within 50 metres of his house.
 
Interesting way to detect presence! How well does this work?

(How often did one of you sit in the dark at home because of this?)
It seemed like the most practical solution. It works surprisingly well; so far the lights have stayed on when we were at home ;)
 
Interesting set up.

You might want to take a look at Home Assistant. It has a bunch of stuff for just this and can support interactive prompts to your phone too.

I used a similar approach, as we moved from full wfh to mixed/hybrid, to spot when none of us were in the house and send an actionable notification (as HA calls them) to my phone I can respond to to turn the heating off for the day. (The prompt was vital - I'd be in a lot of trouble if it accidentally turned the heating off when my wife was at home!)

HA can also work with definable zones and GPS if you have the companion app installed on phones though polling can make it tricky for rapid transitions - a friend wanted to use it to automatically open his garage door when he got within 50 metres of his house.
thanks for the suggestion! I needed something that could work without having to communicate with the connected devices i. e. no apps running on our phones; that would have required active cooperation on my brother's part :)
 
Umm, I just have Alexa set up to turn off various devices including lights when the burglar alarm is set!
 
thanks for the suggestion! I needed something that could work without having to communicate with the connected devices i. e. no apps running on our phones; that would have required active cooperation on my brother's part :)
You can mix and match to suit - it lets you define how presence is set per person. (Or device - I had connectivity issues to the evohome gateway so I set that up as a "person" to track).

For me it's the app and WiFi but just WiFi for the wife - for exactly the reason you mention. Just need the asus integration on HA and it will report all attached devices.
 
I recently scripted up a presence detector. Is there a reason you didn't use ping?

Code:
ping -q -c1 -w1 $IPTARGET > /dev/null
target=$?
 
Very interesting, but I cannot get it to work:
I'm getting this error message:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# tail /jffs/scripts/hueoff.d/logs/log.txt
/jffs/scripts/hueoff: line 88: syntax error: unexpected redirection

Thank you, anyway.
 
Last edited:
Very interesting, but I cannot get it to work:
I'm getting this error message:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# tail /jffs/scripts/hueoff.d/logs/log.txt
/jffs/scripts/hueoff: line 88: syntax error: unexpected redirection

Thank you, anyway.
It's because this script was written in BASH. Our routers are expecting the POSIX flavor (#!/bin/sh) by default. There may be a roundabout way for BASH to work on our routers, but it would take some significant work. It may be a better idea for @kallefornia to try to make his script POSIX-compliant?
 
Very interesting, but I cannot get it to work:
I'm getting this error message:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# tail /jffs/scripts/hueoff.d/logs/log.txt
/jffs/scripts/hueoff: line 88: syntax error: unexpected redirection

Thank you, anyway.
You may have a path problem to your entware version of bash. On my router /bin/bash is symlink'ed to busybox which won't work for this script.

You can fix this by changing the first line of the script to point to your bash. You can locate the location with
Code:
find / -name bash
then change the first line of the script to, for example,
Code:
#!/opt/bin/bash
 
You may have a path problem to your entware version of bash. On my router /bin/bash is symlink'ed to busybox which won't work for this script.

You can fix this by changing the first line of the script to point to your bash. You can locate the location with
Code:
find / -name bash
then change the first line of the script to, for example,
Code:
#!/opt/bin/bash
Well, it doesn't work, either. The first thing I tried was:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# which bash
/bin/bash

And, consequently, I changed the first line in the script to:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# head hueoff
#!/bin/bash

#################################################
#                 HUE AUTO OFF                  #
#                                               #
#             version 1.2.0, 05/2023            #
#                                               #
#################################################

# 05/2023 v. 1.2.0

With no success.
Thank you, anyway.
 
Last edited:
Well, it doesn't work, either. The first thing I tried was:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# which bash
/bin/bash

And, consequently, I changed the first line in the script to:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# head hueoff
#!/bin/bash

#################################################
#                 HUE AUTO OFF                  #
#                                               #
#             version 1.2.0, 05/2023            #
#                                               #
#################################################

# 05/2023 v. 1.2.0

With no success.
Thank you, anyway.

Correct. As I stated, /bin/bash will not work with the script.
 
Well, it doesn't work, either. The first thing I tried was:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# which bash
/bin/bash

And, consequently, I changed the first line in the script to:

Code:
juanantonio@RT-AX86U-6C38:/jffs/scripts# head hueoff
#!/bin/bash

#################################################
#                 HUE AUTO OFF                  #
#                                               #
#             version 1.2.0, 05/2023            #
#                                               #
#################################################

# 05/2023 v. 1.2.0

With no success.
Thank you, anyway.

Thanks for trying it out :)
rung is correct: If you have installed bash via entware, the shebang
Code:
#!/usr/bin/env bash
should, technically, point to the correct location. If not, run
Code:
find / -name bash
to find the correct location and put that in the shebang, for example:
Code:
#!/tmp/mnt/sda1/entware/bin/bash
using
Code:
which bash
won't necessarily give you the correct path.
 
It's because this script was written in BASH. Our routers are expecting the POSIX flavor (#!/bin/sh) by default. There may be a roundabout way for BASH to work on our routers, but it would take some significant work. It may be a better idea for @kallefornia to try to make his script POSIX-compliant?
yes, I am just more comfortable in BASH, so I am running it as my default on my router. I might make a POSIX compliant version in the future; right now I don't have the time 😅. But if anyone fancies it, feel free to transcribe and publish it :)
 
I recently scripted up a presence detector. Is there a reason you didn't use ping?

Code:
ping -q -c1 -w1 $IPTARGET > /dev/null
target=$?
Technically, it's a valid alternative solution, but I have found that ping doesn't work reliably enough: From time to time packets will be lost even though the client is connected.
Plus, I wanted a solution that doesn't actively scan for the devices connected, but rather uses a passive method to check for connected clients.
 
Technically, it's a valid alternative solution, but I have found that ping doesn't work reliably enough: From time to time packets will be lost even though the client is connected.
Plus, I wanted a solution that doesn't actively scan for the devices connected, but rather uses a passive method to check for connected clients.
I guess it is a trade off between taxing the router processor vs. extra network traffic. I have lots of extra network bandwidth but not much extra processing with my router. Also, my clients can connect via an AP by way of a powerline segment so I'm not sure your method would work.

Nice work on the script, btw.
 
I guess it is a trade off between taxing the router processor vs. extra network traffic. I have lots of extra network bandwidth but not much extra processing with my router. Also, my clients can connect via an AP by way of a powerline segment so I'm not sure your method would work.

yes, if the powerline AP acts as its own DHCP server, the router probably won't see those clients :) Although some powerline APs can be configured to connect to the router's DHCP, I think.

Nice work on the script, btw.

thank you 🤓☺️
 
yes, if the powerline AP acts as its own DHCP server, the router probably won't see those clients :) Although some powerline APs can be configured to connect to the router's DHCP, I think
It's not that. I thought you were looking at matching MACs only on the wireless client tables. Clients on my AP would show up as wired clients. Did I get that wrong?
 
It's not that. I thought you were looking at matching MACs only on the wireless client tables. Clients on my AP would show up as wired clients. Did I get that wrong?
Yes, I was only looking in the WiFi tables, but it would easy to add the ethernet interface to the list of scanned adapters, I think.
 

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top