Raspberry Pi Temperature Device

I was interested in keeping an eye on the temperature of my new Raspberry Pi4, so setup a "simple" solution (took me a while to get my head back around some Linux concepts I have used for about 4 or 5 years now).

The general steps were:

On the HE hub:

Setup a virtual temperature device in HE, noting the device Id
Install and configure Maker API or use an existing install
Add new virtual temperature device in the list of devices in Maker API
Copy the example command URL from the Maker API setup screen

On the Raspberry Pi:
Open the crontab editor
sudo crontab -e
If prompted, choose your text editor, I use Pico or Nano for most things.

EDIT: My original post of this text was missing some character in it's display on screen because of the backtick being detected a formatting option. I have now changed it to preformatted text. Good pick up @duane.and.
EDIT2 (17th Sep '22): I have also tweaked the original schedule that I posted, 1 * * * * does not work, changed it to all asterisks.

* * * * * curl http://<HE-IP-Address>/apps/api/<Maker-API-App-Id>/devices/<Device-Id>/setTemperature/`echo "scale=3; \`cat /sys/class/thermal/thermal_zone0/temp\` / 1000" | bc`?access_token=<Maker-API-Access-Token>

EDIT: Also from @duane.and 's post below, I have also included a version of this cron setup for those who don't have bc (basic calculator) installed on their rpi:
EDIT 2 (17th Sep '22): Tweaked the command so that the backticks appear in the post for people to copy
EDIT3 (17th Sep '22): I have also tweaked the original schedule that I posted, 1 * * * * does not work, changed it to all asterisks.

I have the added benefit that I am sending HE events to Influxdb / Grafana on the Raspberry Pi, so can also chart the results. Admittedly a bit of a round trip to get the results back onto the same pi, but simpler than writing code myself. Also not as feature-rich as the pi stats solution from this community. I'll probably get to installing this eventually.

Simon

1 Like

Another option if you have Node Red on your RPi is to use the Hubitat Nodes (node-red-contrib-hubitat) and node-red-contrib-cpu Nodes. The later will give you temperature and the core speed(s) of the RPI that is running Node Red. You also create the virtual HE temperature device as in your method.

If you create a virtual device with temperature and a speed attribute (or 4 speed attributes for case of RPi 4 that I use), then you could easily have that information go into HE. I haven't done it though.

EDIT: When i said "virtual device w/ temperature and a speed attribute", i meant you would have to create your own custom driver for that.

3 Likes

Thanks for this! Small adjustment --> The "bc" (basic calculator) is not installed on my Pi board, and I think there might be a syntax error on the original post... probably a formatting conflict with the backtick character.

Here is the crontab entry that I am successfully using (without bc):
1 * * * * curl http://[IP address]/apps/api/110/devices/[dev ID]/setTemperature/`cat /sys/class/thermal/thermal_zone0/temp | awk '{print $0/1000}'`?access_token=[token]

Substitute IP address, dev ID, and token accordingly.

1 Like

I suspect it was to do with passing the result to bc, i'm not that familiar with that syntax, was definitely a lot of trial and error when I set it up. Wishing now I'd quoted the web site(s) I used. Glad to hear you got it working.

Yeah, looks like my original post doesn't match my cron setup, apologies if it caused you too much of a headache. I'll try and correct it now... Done (Corrected). Thanks again.

Simon

This may be solved but just in case, here is the script I use to get temp from a RPi.
indent preformatted text by 4 spaces
#!/bin/bash

if [ -f /sys/class/thermal/thermal_zone0/temp ] ; then
if [ -x /usr/bin/bc ] ; then
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(echo "$cpu / 100 * 0.1" | bc)
cpuf=$(echo "(1.8 * $cpu) + 32" | bc)
TEMPERATURE="$cpu C ($cpuf F)"
echo "CPU temperature: $TEMPERATURE"
fi
fi

exit 0

Sorry about the formatting.

1 Like

@sburke781 OT but would it be possible to modify what you are doing for temp to use maker API to say when device x detects motion (motion sensor in HE) execute sudo /usr/bin/hyperiond -d on the pi?

I'm pretty sure you could do something like that, but it would be different to what I have setup here.

With this rpi temperature reading the scheduled job on the rpi is calling an address made available on the HE hub by the Maker API app. From what you are asking, the Maker API, i.e. your HE hub, needs to call an HTTP endpoint on the rpi, which then accepts that call and responds by running the command you want. You would most likely not need to use Maker API at all, I believe you can make HTTP calls from Rule Machine, e.g. trigger a rule on motion being detected, with an action of a HTTP call. So I expect all the elements of this are possible, but you may need to find how best to setup something on the rpi end, e.g. so some kind of web server setup or general service that is listening for those calls. Again, that would be possible but need some development to achieve. I'd offer to help, but I have a bad habit of that and taking way too long to follow through, so I'd suggest posting another thread to ask for suggestions if you want to pursue the idea. There are most likely other options than the one I have put forward.

Hope this helps.
Simon

EDIT: Without knowing if this is a valid option, you may want to look at whether Node RED could do what you need, i.e. accept a HTTP call and respond...?

Thanks. I couldn't figure out how to pass a sudo command (a few actually) through the http call.

I ended up using Tasker: autonotification to intercept a push notification from webcore when there is motion and then it triggers a task using autoinput and raspcontroller to use the built in custom commands to write on the command line. The issue with this is that raspcontroller has ads so I have a autoinput close the ad screen but that can be hit or miss. Also the commands don't always execute together.

For you temp script I use a different approach in using grafana with RPI/influx and embed that into a dashboard. This gives me access to more data. I had Smart things devices but have not tried hubitat devices.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.