New Plugin: SiteSensor

Your trip expression is actually being an assignment, not a comparison. Use the “==” operator rather than single “=”.

Was wondering if this plugin could be used to get electricity price data from a site (https://www.nordpoolgroup.com/Market-data1/Dayahead/Area-Prices/FI/Hourly/?dd=FI&view=table) and then use the data in PLEG?

Another option would be to use lua code, but not sure how it would be done. A draft below

local https = require("ssl.https")
local ltn12 = require("ltn12") 
-- 5 Second timeout
https.TIMEOUT = 5
 
  local response_body = {}
  local request_body = ''
 
  local r, c, h = https.request{
    url = 'https://https://www.nordpoolgroup.com/Market-data1/Dayahead/Area-Prices/FI/Hourly/?dd=FI&view=table',
    method = "GET",
    headers = {
      ["Content-Length"] = string.len(request_body),
      ["Content-Type"] = "application/x-www-form-urlencoded"
    },
    source = ltn12.source.string(request_body),
    sink = ltn12.sink.table(response_body)
  }

Not directly in the form given at the URL in your code (which contains an error–two https protocol specs in a row). There is an API you can sign up for, here: [url=https://www.nordpoolgroup.com/TAS/api/]https://www.nordpoolgroup.com/TAS/api/[/url]

…but… the day-ahead API call (which is free, apparently) comes back in XML, and SiteSensor currently does plain text and JSON only, no XML as yet. I would be happy to look at XML parsing though, but it’s not something I can commit to supporting tomorrow, or likely even next week.

If we could get JSON, then SiteSensor could do what you want immediately (grab the data, pass it to PLEG). That API just doesn’t do JSON for that data.

There’s a simple xml coder/encoder as part of openLuup. If you’ve got an example of the response XML, then I could see if it’s up to the job.

Or if that’s not the issue, just say so!

No, the issue is that the output of that parser is usable for us programmers but a nightmare for anyone else, and I’m pretty sure that most users will not have an easy go figuring out how to map the resulting data into an expression that gets them the values they want reliably. That then, makes it turn into a support nightmare for me.

1 Like

So why not translate XML → Lua → JSON?

Take, for example, a possibly familiar bit of XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
    <specVersion>
        <major>1</major>
        <minor>0</minor>
    </specVersion>
    <device>
        <deviceType>urn:schemas-toggledbits-com:device:SiteSensor:1</deviceType>
        <staticJson>D_SiteSensor1.json</staticJson>
        <friendlyName>Site Sensor</friendlyName>
        <manufacturer>rigpapa</manufacturer>
        <manufacturerURL>http://www.toggledbits.com/sitesensor/</manufacturerURL>
        <modelDescription>Site Sensor</modelDescription>
        <modelName>Site Sensor</modelName>
        <handleChildren>0</handleChildren>
        <Category_Num>12</Category_Num>
        <serviceList>
          <service>
            <serviceType>urn:schemas-toggledbits-com:service:SiteSensor:1</serviceType>
            <serviceId>urn:toggledbits-com:serviceId:SiteSensor1</serviceId>
            <SCPDURL>S_SiteSensor1.xml</SCPDURL>
          </service>
          <service>
            <serviceType>urn:schemas-micasaverde-com:service:SecuritySensor:1</serviceType>
            <serviceId>urn:micasaverde-com:serviceId:SecuritySensor1</serviceId>
            <SCPDURL>S_SecuritySensor1.xml</SCPDURL>
          </service>
          <service>
            <serviceType>urn:schemas-micasaverde-com:service:HaDevice:1</serviceType>
            <serviceId>urn:micasaverde-com:serviceId:HaDevice1</serviceId>
            <SCPDURL>S_HaDevice1.xml</SCPDURL>
          </service>
        </serviceList>
        <implementationList>
          <implementationFile>I_SiteSensor1.xml</implementationFile>
        </implementationList>
    </device>
</root>

this simple bit of code:

local xml = require "openLuup.xml"
local json = require "openLuup.json"

local lua = xml.decode(x)
print (pretty(lua))

local j = json.encode (lua)
print (j)

produces this Lua:

{root = {
	 	 device = {
	 	 	 Category_Num = "12",
	 	 	 deviceType = "urn:schemas-toggledbits-com:device:SiteSensor:1",
	 	 	 friendlyName = "Site Sensor",
	 	 	 handleChildren = "0",
	 	 	 implementationList = {implementationFile = "I_SiteSensor1.xml"},
	 	 	 manufacturer = "rigpapa",
	 	 	 manufacturerURL = "http://www.toggledbits.com/sitesensor/",
	 	 	 modelDescription = "Site Sensor",
	 	 	 modelName = "Site Sensor",
	 	 	 serviceList = {service = {{
	 	 	 	 	 	 SCPDURL = "S_SiteSensor1.xml",
	 	 	 	 	 	 serviceId = "urn:toggledbits-com:serviceId:SiteSensor1",
	 	 	 	 	 	 serviceType = "urn:schemas-toggledbits-com:service:SiteSensor:1"
	 	 	 	 	 },{
	 	 	 	 	 	 SCPDURL = "S_SecuritySensor1.xml",
	 	 	 	 	 	 serviceId = "urn:micasaverde-com:serviceId:SecuritySensor1",
	 	 	 	 	 	 serviceType = "urn:schemas-micasaverde-com:service:SecuritySensor:1"
	 	 	 	 	 },{
	 	 	 	 	 	 SCPDURL = "S_HaDevice1.xml",
	 	 	 	 	 	 serviceId = "urn:micasaverde-com:serviceId:HaDevice1",
	 	 	 	 	 	 serviceType = "urn:schemas-micasaverde-com:service:HaDevice:1"
	 	 	 	 	 }}},
	 	 	 staticJson = "D_SiteSensor1.json"
	 	 },
	 	 specVersion = {
	 	 	 major = "1",
	 	 	 minor = "0"
	 	 }
	 }}

and then this JSON:

{"root":{
    "device":{
      "Category_Num":"12",
      "deviceType":"urn:schemas-toggledbits-com:device:SiteSensor:1",
      "friendlyName":"Site Sensor",
      "handleChildren":"0",
      "implementationList":{"implementationFile":"I_SiteSensor1.xml"},
      "manufacturer":"rigpapa",
      "manufacturerURL":"http://www.toggledbits.com/sitesensor/",
      "modelDescription":"Site Sensor",
      "modelName":"Site Sensor",
      "serviceList":{"service":[{
          "SCPDURL":"S_SiteSensor1.xml",
          "serviceId":"urn:toggledbits-com:serviceId:SiteSensor1",
          "serviceType":"urn:schemas-toggledbits-com:service:SiteSensor:1"
        },{
          "SCPDURL":"S_SecuritySensor1.xml",
          "serviceId":"urn:micasaverde-com:serviceId:SecuritySensor1",
          "serviceType":"urn:schemas-micasaverde-com:service:SecuritySensor:1"
        },{
          "SCPDURL":"S_HaDevice1.xml",
          "serviceId":"urn:micasaverde-com:serviceId:HaDevice1",
          "serviceType":"urn:schemas-micasaverde-com:service:HaDevice:1"
        }]},
      "staticJson":"D_SiteSensor1.json"
    },
    "specVersion":{
      "major":"1",
      "minor":"0"
    }
  }}

…and you’re back to what you need?

Thank you for the changes! It has taken a while for me to get back around to this. First my Vera did not want to update, but now it has the changes. I have created some scenes which change state based upon response.value > X and will see how they work. What are the drawbacks or how can I have it query things more frequently than 60sec? I am querying my own EmonPi inside my LAN. I don’t what to overload things, but more frequent than 1 min would be nice.

rearden

[quote=“rigpapa, post:27, topic:197026”]OK, I can fairly easily expand on SiteSensor’s scene options to make this easier. I’m getting ready to an update this weekend, which would publish Monday if Vera stays to their usual schedule.

For the moment, I recommend making an additional SiteSensor device (go to My Apps, click on Details where SiteSensor is listed, and click the “Create another” button. Have one instance trigger when watts > X, and the other trigger when watts < Y. Then have your “high watts” scene trigger from the first instance going into triggered state, and your “low watts” scene trigger when the second instance goes into triggered state.[/quote]

The hard lower limit is 60 seconds right now; even if you go in and edit the state variable (Interval) directly, it will still constrain the delay back to 60 seconds. I can change that for a future release. I’ll do a quick change and point you to the file, so you can upload it to your Vera. Give me a few minutes…

maybe a wrong thread to ask, but… does anyone know why online Lua compilers do not seem to support socket commands?

if I have

local http = require("socket.http")

then e.g. ideone.com gives

lua5.3: prog.lua:3: module 'socket.http' not found:
	no field package.preload['socket.http']
	no file '/usr/local/share/lua/5.3/socket/http.lua'
	no file '/usr/local/share/lua/5.3/socket/http/init.lua'
	no file '/usr/local/lib/lua/5.3/socket/http.lua'
...

As I am finding more and more applications for this plugin on openluup, I have a suggestion/feature request:
Could one or two “value.expression” values be shown on the device below device state on the device page?
Right now it displays “Last query succeeded!” on the device page and nothing at all on the frontpage of ALTUI.
I would like to be able to select which value to show on both the front page and the device page.

That’s so funny. I just posted the latest version to AltAppStore today along with your request for VirtualSensor, and was thinking exactly the same thing. I think I can spudge that in and post it before I leave for vacation. Stay tuned.

OK. If you install the “Github.stable” version from the AltAppStore, you’ll get the value of the first (#1) expression on the dashboard card. You can fancy it up with strings, formatting, and concatencation… for example, my test site, which grabs the current weather, uses this expression: [tt]response.weather[1].description)+" “+format(”%.0f", response.main.temp * 9 / 5 - 459.67)+“°”[/tt] and so the dashboard card is currently displaying [tt]clear sky 73?[/tt].

There’s a full write-up of LuaXP’s (the expression parser) syntax and function library on my web site: LuaXP | toggledbits

Works great! Thank you.
Maybe something specific to ALTUI though. It is not showing on the “Favorite” dashboard, the homepage of ALTUI which shows only your favorite devices. I see a few apps which are showing variables there. Would be great if that same text could also be shown on that page.

It is showing for me. Maybe a hard refresh/cache flush on your browser?

I tried on various browser after a cache reset with the same result. See attached… This is odd. I can see it on the device page but not here

Hmm. I’m not super-familiar with AltUI. On my system, Favorites is the same as the device list, just a subset of devices. There’s also this thing called “My Home” that shows some devices as well, although on mine they are not the devices marked as favorites, and some have values and some do not, but the presentation is different from yours. Screen shots attached.

Ohh I see, we were talking about different things. These work for me and are under the “device” menu. What I am referring to is the frontpage which you get to when you first open the browser to the ALTUI page. There you would only see favorite devices. You can access it by clicking on the ALTUI logo on the top left. You would typically only see the weather widget but if you set some devices as favorites you will see them there too. The favorites seem to be stored in the browser cache though.

Ha! I’d never noticed that, because up until today, I had never marked any devices as “Favorite” in AltUI. I do see it now.

Let me poke around.

OK. Update to the latest Github.stable. That was an interesting distraction. Definitely not the easiest part of AltUI to work in; there are a lot of style side-effects to the box model/inherited css being used there. Side-effect of those draggable widgets, it seems. I’m not going to spend a lot of time working around it, but what’s there should suffice. Actually looks much better on my phone than on my desktop.

Good enough for my purpose. It takes out the icon and replaces it with the string…