JSON
KC Hundere
khundere at gmail.com
Sun Jun 20 13:47:55 EDT 2021
In another use of this script I receive a bunch of info from a power
monitor. Is there a way I can loop through this like an array? Something
Like:
repeat with JSONData
set unit property *key* in unit (thisUnit) *value*
end repeat
Here is the JSON payload:
{
TotalStartTime:"2020-02-04T03:57:27",
Total:"0.43",
Yesterday:"0.013",
Today:"0",
Period:0,
Power:0,
ApparentPower:0,
ReactivePower:0,
Factor:"0",
Voltage:121,
Current:"0",
unit:"POD1"
}
On Wed, Jun 16, 2021 at 12:09 PM KC Hundere <khundere at gmail.com> wrote:
> Of course using your on scripts just worked. I don't know why mine didn't
> but as my daddy always said, "If it aint broke, don't fix it". I was also
> able to include the try block, final on script below.
>
> Is there a way to light the battery flag icon from the value in the on
> script?
>
> on JSONRequest(formData, JSONData)
>
> --describe appleevent JSONData
>
> try
>
> set moist to moisture of JSONData
>
> set battery to battery of JSONData
>
> set temp to |temperature| of JSONData
>
> set conduct to conductivity of JSONData
>
> set light to light of JSONData
>
> --write log "moist is " & moist
>
> set value of (thisUnit) to moist
>
> Set Unit Property "Temperature" in unit (thisUnit) to temp
>
> Set Unit Property "Conductivity" in unit (thisUnit) to conduct
>
> Set Unit Property "Light" in unit (thisUnit) to light
>
> Set Unit Property "Battery" in unit (thisUnit) to battery
>
> --write log "set value is complete"
>
> on error
>
> write log "Something is amiss" color red
>
> end try
>
> send reply "success" return code 200 content type "text/plain"
>
> end JSONRequest
>
> On Wed, Jun 16, 2021 at 9:25 AM James Sentman <james at sentman.com> wrote:
>
>> I’m having trouble duplicating this issue. When I run this it works fine
>> but there may be some scripting things.
>>
>> First I would use the “describe appleevent” verb instead of write log,
>> that will show you exactly what the layout of the JSONData record is.
>> Something like this and give it a hit:
>>
>> on JSONRequest( formData, JSONData)
>> write log “in JSON Request”
>> describe appleevent JSONData
>>
>> end JSONRequest
>>
>> on my machine the output from that looks something like:
>>
>> 'misc'\'DApl'{ '----':{ 'usrf':[ 'utxt'("moisture"), 90 ] },
>> &'subj':null(), &'csig':65536 }
>>
>> and that is what I would expect. Thats a user record with one field named
>> moisture with a value of 90. If that output looks similar then the problem
>> is something else in the script which may be being hidden by the onerror
>> handler. If you remove that then the actual error will get sent to the log
>> and we can see what it’s really saying the error is. So then try something
>> like this:
>>
>> on JSONRequest(formData, JSONData)
>>
>>
>> describe appleevent JSONData
>>
>> set moist to moisture of JSONData
>> write log “moist is “ & moist
>>
>> set value of (thisUnit) to moist
>>
>> write log “set value is complete"
>>
>> send reply "success" return code 200 content type "text/plain"
>>
>>
>> end JSONRequest
>>
>>
>> and post the output of that in the log or the text of any errors that
>> popup.
>>
>>
>> On Jun 15, 2021, at 6:00 PM, KC Hundere <khundere at gmail.com> wrote:
>>
>> I am trying to send JSON data from a plant monitor to Xtension using the
>> HTTP/JSON interface and the example from machomeautomation.com.
>> Xtension responds with success, but the data does not get to the unit. Any
>> pointers would be appriciated. I've simplified it to the two following
>> examples:
>>
>> Simple python script
>> import requests
>> data = {"light": 97, "temperature": 28.2, "moisture": 0, "conductivity":
>> 0, "battery": 100}
>> r = requests.post("http://10.1.1.5:21234/40787C165942/plant",
>> json={'json_payload': data})
>>
>> Curl from command line
>> curl --header "Content-Type: application/json" --request POST --data
>> '{"moisture": 90}' http://10.1.1.5:21234/40787C165942/plant
>>
>> With either one, the data does not get to the unit. With debug on I get
>> the following in the log:
>>
>> Jun 15, 2021 2:31:28 PM HTTP/JSON server v1.5.6 (HTTP Server): JSON
>> connection from: 10.1.1.72
>> Jun 15, 2021 2:31:28 PM HTTP/JSON server v1.5.6 (HTTP Server): json
>> request (/40787C165942/plant) token=(40787C165942) address=(plant) value=()
>> Jun 15, 2021 2:31:28 PM
>> Jun 15, 2021 2:31:28 PM
>> Jun 15, 2021 2:31:28 PM JSON data did not have an event parameter.
>> Jun 15, 2021 2:31:29 PM HTTP/JSON server v1.5.6 (HTTP Server): sending
>> reply 200 - success
>> Jun 15, 2021 2:31:29 PM HTTP/JSON server v1.5.6 (HTTP Server): HTTP/1.1
>> 200 OK
>> Jun 15, 2021 2:31:29 PM Date: 2021-06-15 14-31-29
>> Jun 15, 2021 2:31:29 PM Server: XTensionJSON/19
>> Jun 15, 2021 2:31:29 PM Cache-Control: no-cache
>> Jun 15, 2021 2:31:29 PM Connection: close
>> Jun 15, 2021 2:31:29 PM Content-Length: 7
>> Jun 15, 2021 2:31:29 PM Content-type: text/plain
>> Jun 15, 2021 2:31:29 PM Content-Language: en
>> Jun 15, 2021 2:31:29 PM HTTP/JSON server v1.5.6 (HTTP Server):
>> SendComplete, closing the connection
>>
>> On script for the unit "plant"
>> on JSONRequest(formData, JSONData)
>> write log JSONData
>> write log formData
>> try
>> set moist to moisture of JSONData
>> set vaule of (thisUnit) to moist
>> write log "moisture: " & moist
>>
>> on error
>>
>> write log "JSON data did not have an event parameter." color red
>>
>> end try
>>
>> send reply "success" return code 200 content type "text/plain"
>>
>> end JSONRequest
>>
>> --
>> KC Hundere
>> _______________________________________________
>> XTensionList mailing list
>> XTensionList at machomeautomation.com
>> http://mail.machomeautomation.com/mailman/listinfo/xtensionlist
>>
>>
>> Thanks,
>> James
>>
>>
>> James Sentman http://www.PlanetaryGear.org
>> http://MacHomeAutomation.com
>>
>>
>>
>>
>> _______________________________________________
>> XTensionList mailing list
>> XTensionList at machomeautomation.com
>> http://mail.machomeautomation.com/mailman/listinfo/xtensionlist
>>
>
>
> --
> KC Hundere
>
--
KC Hundere
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.machomeautomation.com/pipermail/xtensionlist/attachments/20210620/a7d87a20/attachment.html>
More information about the XTensionList
mailing list