LUA code question

I created a LUA sequence that retrieves json from my local weather station running WeeWx. I based the code off of a weather example @RickCarlino posted. Basically I want to use the rain data and prevent my watering sequence from running if there has been rain. I have that part working but my question is now what?

Ideally, I’d like to have an if statement that checks if it has rained and if it hasn’t, run my watering sequence and if it has rained, just send me a message telling me the watering sequence didn’t run due to rain but I’m unsure how to proceed with he current implementation. I don’t see a way to set a variable to use in an if statement, etc.

  resp, err = http({
    verb="GET",
    url="http://10.0.0.229/weewx.json"

if err then
  return send_message("error", "Weather error.")
end
weather = json.decode(resp.body)
rain = tonumber(weather.day.rain_total.value)

if rain > 0 then
  
  send_message("info", "It rained today: " .. rain, "toast")
else
  send_message("info", "No rain today: " .. rain, "toast")
end

Hi @stre1026 .

ASSERTION commands in a Sequence execute a Lua “truthy” function ( i.e. function result is Boolean ) The documentation is readable right from the Web App in

  • Software Documentation
    • Advanced Sequence Commands
      • ASSERTION

I’m not sure how to propagate some object out of that ASSERTION command to use elsewhere in your Sequence. Lead Developer @RickCarlino should be able to clear that up :slight_smile:

This looks like a fun project @stre1026 ! @jsimmonds is correct- the ASSERTION command will get the job done for what you are trying to do - let us know if you need further assistance or if there are parts of the documentation that are hard to understand. I hope to build a more robust solution in the future, but ASSERTION should get the job done.

Sharing Lua Data

The best way to store state between runs is with the env() helper.

env("name", "value") stores a string as “name” for use later.
env("name") retrieves a previously stored env by name.

Store the Data

You can store the data in the ENV, as JSON:

weather = {temperature = 1.23}
json_weather = json.encode(weather)
env("weather_report", json_weather)

Fetch Data

Now that the weather report is in the env(), we can retrieve the JSON:

raw_string = env("weather_report")
weather_report = json.decode(raw_string)
send_message("info", weather_report.temperature, {"toast"})

Does that help? I look forward to hearing more about your project!

Thanks @jsimmonds and @RickCarlino

Sorry if I sound dumb, but I’m having a hard time wrapping my head around how this would all tie together. So my script to get the JSON works perfectly. It polls the weather station for the JSON and then I have the if statement that looks like this:

if rain > 0 then
  env("rain", "yes")
  send_message("info", "Rain Today", "toast")
else
  env("rain", "no")
end

Now how does the assertion work? I have an IT background and have taken many programming courses in college so I have a working knowledge of programming but I’m far from an expert. The way I instinctly want to handle this is that I want to have an if statement and run the watering sequence if rain == no else send an E-mail if rain is yes telling me it skipped so I essentially want to run one sequence if yes or another if not. How would go about that?

@stre1026 You probably want to do something like this:

image

Here’s how it works:

  • The ASSERTION block is used to check if a condition is OK, Safe, etc…
  • You must return true to indicate the condition is OK.
  • You return false to let the system know the assertion has “failed”.
  • Once the system knows if the condition is OK / FAILED, it will either continue execution (no rain) or attempt to recover from the situation (water plants in the absence of rain).

So in your case, you would indicate a “safe” condition based on whether or not it is raining. No rain is considered an “unsafe” condition that requires recovery steps (irrigation).

The original purpose of the ASSERTION block was for FarmBot employees to run built-in self-tests on the hardware, but it turned out that the feature was extremely useful to end-users, also. Hopefully, that bit of trivia will give you a better understanding of why it exists, how to use it, etc.

Does that make sense? Happy to clarify if not.

Hi @RickCarlino

Ah, this makes sense now. So I would essentially build a sequence that I will run scheduled daily that pulls the JSON with my LUA, then put this assertion block in the same sequence that will allow it to decide whether or not to water? Is there a way to run a sequence if it does rain as well? I want to change an LED light tower I have (like on large commercial CNC machines) to red so I know visually.

Correct. You might even be able to put all the logic into one ASSERTION block.

You could put a second assertion block after the first one. If any data needs to be passed between the two blocks, you could store the data in an env().

This is a great idea! I have an extra wooden post in the garage… this might be a fun spring project for my bot.

Thanks @RickCarlino

I will try and combine everything together.

I have one of these Allen Bradley light towers I bought on eBay. It happens to be 24v so my plan is to use the extra 2 power ports on the Farmduino to control the red and green LEDs on this to visually show me the status of the machine. The controller box is mounted on the opposite side of of the gantry from where I view the FarmBot so I can’t easily see the built in LEDs on the control box so I will mount this to the opposite side of the gantry.
image

1 Like