Electricity price based control of IoT devices in LuCI

I’d like to implement simple control of IoT devices based on the electricity price data for the current and the next day. I am aware of alternatives such as HomeAssistant and various proprietary clouds of IoT device manufacturers.

Last year, I wrote a shell script that made use of the jshn library to parse JSON price data and to schedule the cheapest time slot for a warm water boiler. I implemented an improved version of the logic in an ESP-32 based Tasmota device (pull request). Now I would want to port this to LuCI.

I see that many of the graphs in the LuCI web interface for CPU usage, bandwidth, connections, and so on are based on data that will be read from files in the directory /var/lib/luci-bwc by a C program luci-bwc.

I’d want to present a graph similar to https://sahkotin.fi or https://dashboard.elering.ee/et/nps/price and some user interface for highlighting the time slots when power will be used, as well as setting some parameters for the price optimization.

For a ‘price database’ I could simply use a JSON file in the file system. Every 15 minutes, a Lua or shell script would be invoked to parse the file, to write it back with any past entries removed, and to initiate action when needed. For example, an action could be sending an HTTP GET request to turn on a heater. About once per day, the script would try to refresh the price data from an online data source. Part of the code would be interacting directly with the web browser, such as sending the data for drawing the price graph.

I’d be grateful for any pointers, such as LuCI extensions that try to achieve anything similar.

1 Like

hass has those optimizing algorithms included, go for it.

2 Likes

Just to clarify, I want to minimize the amount of always-on hardware, and I would not dare to run too complex software on my router. The algorithm is rather trivial. Basically we only need a binary heap based priority queue or ‘lazy sort’ that determines the cheapest N time slots out of the total available number of slots. The result would have to be sorted in ascending order of timestamp. I see that Lua implementations of binary heap already exist. It could also be feasible to sort all time slots by price and pick and sort the first N times.

My question was mainly how to integrate one’s own code in LuCI. https://github.com/openwrt/luci/wiki/ModulesHowTo looks like a promising starting point.

I learned that an effort to migrate LuCI from Lua to server-side Javascript started in 2022. https://openwrt.github.io/luci/ and https://zhanzat.github.io/luci-js-cookbook/ look useful. Maybe the entire application could be written in Javascript.

I just learned that https://ucode.mein.io is the server side LuCI script interpreter. Its uloop binding would seem to allow me to implement the necessary 15-minute timer intervals. There is also a socket module, which is perfect for interacting with IoT devices in the LAN, such as http://tasmota.lan/cm?cmnd=POWER+ON. For updating the price information from an external HTTPS source, it should be simplest to invoke curl or wget via popen.

For the web browser, I think I will adapt the bandwidth graph of luci-mod-status. Overall, luci-app-example is a good starting point, although this document about parsing YAML has not been adjusted as part of the transition from Lua to ucode.