Hyperion with Sunrise and Sunset

You may have gotten here from my hyperion with nagios writeup, this doesn’t follow on from that and is separate, but maybe of interest.

The basic idea: I’ve now got LED’s in my room and would like them to come on before I go to bed so I can see without falling over. The ones on the stair I just leave running, but like hell am I going to sleep with such a bright LED (I probably could, I can sleep in the day but I thought it would be a better idea for them to come on ready).

I could have just set this up on a basic cron and picked a time early enough to account for summer/winter before I go to bed. but where’s the fun in that. I know my PI can work out when the sunrise/sunset is. so it can’t be that difficult to set something up.

After a little bit of searching I come across sunwait you will need this or a similar program. I wont cover installing sunwait on the PI here. just the config I use with hyperion.

First I need a script that sunwait will call and tell it what it needs to do. Here’s my sun-light.sh

#!/bin/bash

COMMAND=hyperion-remote
COMMAND_PRIORITY=50
COMMAND_PATH="/usr/bin"
case "$1" in
sunset)
        /usr/bin/hyperion-remote -p 50 -e "Knight rider"
;;
sunrise)
        /usr/bin/hyperion-remote -p 50 -e "Little Chaser Blue"
;;

*)
        echo "Usage: $0 {sunrise|sunset}"
        exit 1
esac

exit 0

Don’t forget to make this script executable ‘chmod +x sun-lights.sh’

This is basically told to either run sunset or sunrise and will then call hyperion-remote passing the relevant priority and effect. (Little Chaser Blue is a copy and customisation of Knight Rider)

Then I added the following to /etc/crontab

0 02   * * *   root    sunwait -p sun up 51.xxxxN 3.xxxxW ; /root/sun-lights.sh sunrise
12 02   * * *   root    sunwait -p sun down 51.xxxxN 3.xxxxW ; /root/sun-lights.sh sunset

This basically run’s sunwait which will wait until the sun is either coming up or going down at the specified co-ordinates before running the bit after ;

The important bit to get your head around if you must have cron run this at a time well before the sun will rise or set. midnight and midday seems like a good safe bet.

I know I’ve skipped over the actual installation of sunwait and more details on hyperion and running the scripts to check it works, but it’s 1am and I just want to save this 🙂 So if you’ve got this far and are still confused, comment below and I’ll expand on the relevant bits.

Hyperion LED’s & Nagios (Part 3)

Hopefully you’d read Part 2. If not you’ll need to or this wont work.

So in this part we’re going to setup the nagios stuff to set off the alert LED’s. As a little bit of background my nagios installation is on a completely separate PI to the hyperion LED’s, but I have install hyperion on this pi to make use of hyperion-remote. Yes I was being lazy I could have used other methods instead of installing the whole thing.

First my nagios installation is in ‘/usr/local/nagios’, I’m not going to go through the commands to cd and edit, if you’ve installed and configured nagios I’ll assume you can do them 🙂

This is my /usr/local/nagios/libexec/notify-hyperion.sh

#!/bin/bash
STATE=$1
DURATION=23000
case $STATE in
"CRITICAL")
   EFFECT="Red Alert"
   ;;
"WARNING")
   EFFECT="Yellow Alert"
   ;;
"OK")
   EFFECT="Green Alert"
   ;;
*)
   ;;
esac

hyperion-remote -a osmc-l:19444 -d $DURATION -p 10 -e "$EFFECT" &
hyperion-remote -a rasp-light:19444 -d $DURATION -p 10 -e "$EFFECT" &
hyperion-remote -a webcam-pi:19444 -d $DURATION -p 10 -e "$EFFECT" &

For the nagios saavy amongst you, you’ll see I account for CRITICAL, WARNING & OK. Yes I do need to add DOWN, UNREACHABLE & UP for the host alerts

The DURATION sets how long in ms hyperion will run this effect for (best worked out in conjunction with the speed, freq & step from the hyperion config. I’ve got this just right to cut off the alert after (I think) 4 fades. I force the priority ‘-p 10’ to 10, anything else I do with hyperion generally has a priority 50, 100 or 1000 so these will take over.

The last 3 lines are 1 each for my hyperion installs, you will need to change the name’s or replace them with the IP addresses dependant upon your network configuration.

Don’t forget to make the script executable, and you can test it with ‘./notify-hyperion.sh OK’

With the above tested and working, I’ve added the following to my nagios command.cfg

# 'notify-host-by-hyperion' command definition
define command{
        command_name    notify-host-by-hyperion
        command_line    /usr/local/nagios/libexec/notify-hyperion.sh "$HOSTSTATE$"
}

# 'notify-service-by-hyperion' command definition
define command{
        command_name    notify-service-by-hyperion
        command_line    /usr/local/nagios/libexec/notify-hyperion.sh "$SERVICESTATE$"
}

Then added the following to contacts.cfg

define contact{
        contact_name                    nagios-hyperion
        alias                           Nagios Hyperion
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r,f
        host_notification_options       d,u,r,f,s
        service_notification_commands   notify-service-by-hyperion
        host_notification_commands      notify-host-by-hyperion
        }

define contactgroup{
        contactgroup_name       nagioshyperion
        alias                   Nagios Hyperion Notifications
        members                 nagios-hyperion
        }

For my installation I’ve then added

contact_groups                  nagioshyperion

To my template’s. You could add this to each service/host.

Within my setup I’ve stopped using email alerts, so changing the contacts to hyperion was fine. Within the templates I then have the notify_interval setup for 15 minutes. This means it will fire an alert to hyperion every 15 minutes. If you use email on your system too, you may not want to do this. an alternative could be changing the duration above, so that the red and yellow alerts are constant and the green run’s for a limited time before clearing down.

I did contemplate using event filters instead of contacts, so I could have the emails turned back on at some point, but decided against it as I would have to check before sending a green alert that it’s not already in green or I’d just end up with green all the time.

After all of the above make sure you restart nagios for the new config to take effect.

As a side note, I was sat watching TV this evening all of a sudden my room was yellow and I thought WTF. I do have hyperion setup in my room to start the LED’s at sunset but it was still light out and shouldn’t have fired. Then it clicked NAGIOS. and yes hey presto nagios had throw this site into warning status as there were updates available. I can see it getting annoying if my ISP drops out and I end up with alerts for hours. but on the whole I’m really happy it works, all I need now is a red alert klaxon 🙂

If your interest in setting up hyperion at sunrise/sunset I’ll be writing that one up separately.

Hyperion LED’s & Nagios (Part 2)

Maybe you read Part 1, maybe you skipped it 🙁

Either way Part 2 is going to cover creating a new effect for Nagios. I wont say it’s the best bit of python programming I’ve done (Yes I copied and changed another effect as a starting point). The main thing I wanted was an alert status (yes Star Trek does come into it).

There’s 4 files to this effect:

  1. alert.py – The actual python program
  2. alert-green.json – etting a green colour (yes it’s spelt COLOUR not color!!!!!)
  3. alert-yellow.json
  4. alert-red.json

My hyperion is installed to ‘/opt/hyperion’ so first thing is to go to the effects directory

cd /opt/hyperion/effects

Then create the alert.py file

nano alert.py

Paste the following into it and save the file

import hyperion
import time
import colorsys
import numpy as np

# Get the rotation time
colorfrom =     hyperion.args.get('colorfrom', (0,0,0))
colorto =       hyperion.args.get('colorto', (0,255,0))
speed   =       float(hyperion.args.get('speed', 0.05))
freq    =       float(hyperion.args.get('frequency', 2.5))
step    =       float(hyperion.args.get('step', 30.0))
ledCount =      hyperion.ledCount

# Setup the colors
colorfromnp     =       np.array(colorfrom)
colortonp       =       np.array(colorto)

#define the lerp calc
def lerp(a, b, t):
        x = a*(1 - t) + b*t
        x = np.around(x)
        x = x.tolist()
        return x

# Start the write data loop
while not hyperion.abort():
        for i in range(0,int(step)):
                n = i / step
                thiscolor = lerp(colorfromnp, colortonp, n)
                colorLedsData = ledCount * bytearray((int(thiscolor[0]), int(thiscolor[1]), int(thiscolor[2])))
                hyperion.setColor(colorLedsData)
                time.sleep(speed)
        time.sleep(speed*10)
        for i in range(1,int(step)):
                n = i / step
                thiscolor = lerp(colortonp, colorfromnp, n)
                colorLedsData = ledCount * bytearray((int(thiscolor[0]), int(thiscolor[1]), int(thiscolor[2])))
                hyperion.setColor(colorLedsData)
                time.sleep(speed)
        colorLedsData = ledCount * bytearray(colorfrom)
        hyperion.setColor(colorLedsData)
        time.sleep(freq)

I do use a numpy array, I can’t remember installing it but may have done. You can test if you have it installed

python
import numpy as np

Then we create the alert-green.json

nano alert-green.json

And Paste

{
        "name" : "Green Alert",
        "script" : "alert.py",
        "args" :
        {
                "colorfrom" : [0,0,0],
                "colorto" : [0,255,0],
                "speed" : 0.05,
                "freq" : 2.5,
                "step" : 30.0
        }
}

alert-yellow.json

{
        "name" : "Yellow Alert",
        "script" : "alert.py",
        "args" :
        {
                "colorfrom" : [0,0,0],
                "colorto" : [255,255,0],
                "speed" : 0.05,
                "freq" : 2.5,
                "step" : 30.0
        }
}

alert-red.json

{
        "name" : "Red Alert",
        "script" : "alert.py",
        "args" :
        {
                "colorfrom" : [0,0,0],
                "colorto" : [255,0,0],
                "speed" : 0.05,
                "freq" : 2.5,
                "step" : 30.0
        }
}

Finally restart hyperion to pickup the new effects

service hyperion restart

That’s it, you should now be able to run these effects. From the terminal:

hyperion-remote -e "Red Alert"

Should start the led’s glowing Red. If not I’d start with ‘hyperion-remote -l’ to check the effects are listed, then move on to working out what’s missing (see above about numpy array).

A few notes on this effect:

  • Originally I programmed it forced to black, but it made sense to change this to a colorfrom value for future (if you want it green and fading to red for example).
  • The speed is how quickly to progress through the fade.
  • The step is how many colours it goes through to get from the colorfrom to the colorto
  • Working out the speed and the step together are important for a nice fade (not jerky).
  • The Freq in how long it stays on black before running the fade again.

This completes the hyperion side of the setup. I now have this effect installed and running on 3 PI’s and yes when there’s a problem they ALL go to yellow/red alert

See Part 3 for the nagios setup