Unable to find libraries in LUA script

I have written a simple script to obtain the cpu temperature and send a message if it’s above 60deg.

local temp = sys.get_cpu_temp()
if temp then
send_message()

I receive the Error ‘nil’ on the first line. I have also tried fbos.get… but don’t know where to look for documentation on what libraries and functions are available out of the box.

Any help on this script and where to find relevant docs is appreciated.

Thanks
Stuart

Hi @stuartturner
FBOS LUA is sandboxed to occlude sys from Sequence statements. (Is that a standard library ?)

The functions made accessible are documented online . . for your CPU temp value you could use

local temp = read_status('informational_settings.soc_temp')

See Configuration | FarmBot Developer Documentation

Thanks for the guidance.

After trying to do this for quite a while, I asked Gemini for help writing the script. It took me down a whole sys library is missing, reinstall path. So the libraries and other things were from that.

Thanks for the line you provided. I think it would have taken me hours to figure it out from the documentation. I don’t find this very intuitive.

Your line does not throw an error but I still don’t know whether I have the temperature from it as I don’t see any output from my toast or send_message commands in logs. Where should I be looking for these messages to appear?

I’m used to writing lots of unit tests. I haven’t used a trial and error approach for over 25 years. Is there a test harness available?

Here’s the script I have:
local temp = read_status(‘informational_settings.soc_temp’)
toast(temp)
send_message(“info”, temp)

Of course I then need a conditional to determine whether to send an email but any assistance in showing the temperature is appreciated.

Thanks

Stuart

You can have the app automatically generate Lua code by using the AI Lua generation feature.

prompt:

write a simple script to obtain the cpu temperature and send a message if it’s above 60deg

result:

-- Read the current CPU temperature (C) from the device status tree
local cpu_temp = read_status().informational_settings.soc_temp

-- Exit if the temperature is unavailable
if cpu_temp == nil then
  send_message("warn", "CPU temperature unavailable.", "toast")
  return
end

-- Send a warning message if the CPU temperature is above 60C
if cpu_temp > 60 then
  send_message("warn", "CPU temperature is high: " .. cpu_temp .. "C", "toast")
end

Output from the send_message function with show up in the logs pop-up, and toast will additionally show a temporary notification in the web app.

1 Like

Hi @stuartturner

Ooops . . I almost always never post sample code that I’ve not actually tested :winking_face_with_tongue:
That statement should have read

local temp = read_status('informational_settings','soc_temp')

Now @Gabriel has provided another solution plus AI code gen. demo !!

Yes, but I think not accessible from within the Web App or the Lua sandbox VM.
@Gabriel is a test code Ninja and will probably have suggestions for you.

There are other properties (ones that you can see at various places in the Web App) that
read_status() can return . . from FarmBot GitHub repos informational_settings.ex

Coincidentally, there’s a recent Issue opened on read_status() read_status() with no arguments crashes the Lua VM with {:badkey, N} · Issue #1526 · FarmBot/farmbot_os

There are tests available in the source code, but besides using the Lua generation feature, the best information about what’s available in read_status() is the developer documentation, which has a link to the typescript bot state interface.

We’ve included some improvements to the Lua code generation feature in the latest software update.

1 Like

Thanks to both @jsimmonds and @Gabriel for helping with this.
I now obtain the cpu temperature and see that displayed on screen.
I don’t receive an email, and I don’t receive emails about posts to this forum, even though my notifications are set to on.

What I realise I want is to log the temperature throughout the day into visible form, such as a spreadsheet and from there a graph.

Any suggestions for how I can export readings to achieve this?
I can then try to get the generator to create the code for it.
Thank you
Stuart

Hi @stuartturner

Check Profile / Preferences / Email setting for “@”-mentions : always ?

Email from your Farmbot device should work with the send_message() function, just be aware that the Web App mailer batches up outbound messages and sends the batch after a 5-6 minute delay.

Processing incoming mail messages to find your temperature readings and then assemble those into CSV seems a bit tedious . . there are other ways of getting data out of a Farmbot device, such as web api POSTs or SSE streams.
A Forum poster recently showed how to use Microsoft Teams / webhooks to do fancy things.

Have you seen the connectivity popup history tab? The data there can be downloaded with the FarmBot Python library via fb.api_get('telemetries') if needed.

If you need to get data out of a FarmBot device in realtime, the HTTP POST to your own server or external service from Lua as @jsimmonds mentioned is a good approach. The sidecar hardware documentation has some examples.

Since CPU temperature is already broadcast over the message broker and saved to the web app, there are many different ways to access the data via an external machine using the FarmBot Python library.

1 Like

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