Rule engine and JSON event payload

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
FanOfHue
Normal user
Posts: 75
Joined: 06 Oct 2018, 10:08

Rule engine and JSON event payload

#1 Post by FanOfHue » 04 Jan 2020, 16:38

After a year of successfully running ESPCoreRules with the rule engine based on ESP Easy, i felt a need for a bit more advanced event messages between devices. These messages should be able to handle additional metadata on a specific event, besides the information that could be obtained from the topic itself.

Instead of falling back to some comma separated solution, i decided to give JSON a try as we are already used to do JSON in publishing to MQTT in some cases.

First need was to obtain a unique ID used by the remote host that generated the event. Currently using the MAC address as this is both available for Ethernet devices as well as Zigbee (Philips Hue) devices that also have a (64 bit) MAC address.

So instead of the most simple incoming event:

Code: Select all

HA/Demo/Temperature=21.5
we could receive a json eventvalue:

Code: Select all

HA/Demo/Temperature={"Value":21.5,"Mac":"00:11:22:33:44:55"}
Of course, current rule engine would not be able to properly handle the JSON object, so we need to parse a value from it. I've added some basic logic to the rule engine to automatically parse a JSON valuepair named "Value" to an internal float value to be used for all the rule logic like = < > etc. Also for "If" statements.

So now i can send basic json objects as eventvalue from a remote system and the local ESP Rule Engine would be able to handle this without a single change to existing rule scripts.

Existing rule like this will still work (with the enhanced rule engine that internally parses the "Value" component):

Code: Select all

on HA/Demo/Temperature>0 do
  if %eventvalue%<50
    do something...
  endif
endon

In my case, a more advanced systems like the WindowsRules engine passes the eventvalue to a powershell script that can fully decode the JSON object and fills a local MS-SQL database with all topics received on the MSGBUS by either UPD or MQTT. Here the Mac address is used as primary key in the database so it needs to be available on each event.
Sample WindowsRules:

Code: Select all

On HA/* do
  shell powershell .\topic.ps1 '%event%
Endon
Sending from ESP or any other device, now goes like this:

Code: Select all

MSGBUS HA/Demo/Temperature={"Value":%Temperature%,"Mac":"%mac%"}
or when using MQTT:

Code: Select all

MQTTpublish HA/Demo/Temperature,{"Value":%Temperature%,"Mac":"%mac%"}
This allows to add additional data to the event without disturbing existing ESP rule engine scripts !

Next step could be to add a "parseFromJSON" rule command that can get addtional JSON values and store them into rule variables, either floats or strings.

Would this be of any interest on ESP Easy as well?
(not that i will likely be able to code it on the latest ESP Easy code, because it seems so much more complicated style of coding compared with R120)

Other question:
While surfing the internet on MQTT/JSON standards, i could not find any form of widely adopted standard structures for topics and JSON payloads related to IOT. So i just started to invent my own at the moment. But wouldn't it be great if there was something like a universal MQTT-IOT message format that will work on (m)any Home Automation system out of the box?
(or at least a subset that is widely understood?)
Anyone experience on this, or just stick with the standard of your chosen controller?
Which would mean a large change when changing controller.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests