Example: Using light sensor data of squeezebox radio in openHAB

Hi all,

the Squeeze radio has a built-in light sensor to dim the display depending on the lighting condition. The sensor’s data can be accessed via SSH and used in openHAB. I thought I’d share my setup in case someone is interested. I am sure, here are more elegant ways to achieve this, but this method works very reliably for me.

  1. Enable SSH access on the Squeezebox Radio:
    In the menu of the squeezbox radio, go to Home --> Settings --> Advanced --> Remote Login --> Enable SSH.
    There should be a message on the screen displaying the root password needed for the login.
    Now when you ssh into the squeezbox radio, you can readout the light sensor’s data using this path:
    /sys/class/i2c-adapter/i2c-1/1-0010/ambient

  2. In openHAB create a Number item for the sensor data:
    Number sbLightSensor "Light Sensor [%d]" <sun>

  3. Get the sensor data (I have this running on a linux machine):
    In order to get the sensor value fed into openhab, I have created a small bash script which is executed regularly using a cron rule (executeCommandLine). The script reads the value of the light sensor and puts it in openHAB using the REST API. You need to adapt the IP of the squeezebox and the root password and also IP and port of the openHAB machine.


> #!/bin/sh
> LIGHTSENSOR=/sys/class/i2c-adapter/i2c-1/1-0010/ambient   # Path of luminance sensor
> SBIP=xxx.xxx.xxx.xxx                                      # IP of squeezebox

> # get sensorvalue from squeezebox
> SENSORVALUE=`ssh -n -j <PASSWORD> -o KexAlgorithms=+diffie-hellman-group1-sha1 root@$SBIP 'cat '$LIGHTSENSOR`
> if [[ $SENSORVALUE == "" ]] || [[ $SENSORVALUE -gt 100000 ]]; then      # Error reading SENSORVALUE; max. SENSORVALUE=100000
>         SENSORVALUE=-1                                                  # Error reading sensor value - set sensor to -1
> fi

> # put value to openhab rest api
> curl --header "Content-Type: text/plain" --request PUT --data $SENSORVALUE http://127.0.0.1:8081/rest/items/sbLightSensor/state

> #echo $SENSORVALUE

Cheers,
Matt

3 Likes

Thank you @mstehle. I was able to set up an exec item to get the info after reading your post:

Number SQ_LightSensor "Squeezebox Light" (Squeezebox) {exec="<[ssh root@XXX.XXX.XXX.XXX -n -i /var/lib/openhab/.ssh/id_rsa_oh -o StrictHostKeyChecking=no cat /sys/class/i2c-adapter/i2c-1/1-0010/ambient:300000:REGEX((.*?))]"}

2 Likes