Rule engine and JSON event payload

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
FanOfHue
Normal user
Posts: 96
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.

tim
Normal user
Posts: 52
Joined: 05 Dec 2015, 11:24

Re: Rule engine and JSON event payload

#2 Post by tim » 25 Jan 2020, 10:50

I use ESPcorerules on my LCS doorsensor and i am happy the way it works now (really fast)
Do i need to use this new JSON way of work?

Will it also work with ESPEasy as gateway?

FanOfHue
Normal user
Posts: 96
Joined: 06 Oct 2018, 10:08

Re: Rule engine and JSON event payload

#3 Post by FanOfHue » 29 Jan 2020, 20:18

tim wrote: 25 Jan 2020, 10:50 I use ESPcorerules on my LCS doorsensor and i am happy the way it works now (really fast)
Do i need to use this new JSON way of work?

Will it also work with ESPEasy as gateway?
For the sensor, using JSON does not add a real benefit.

But i'm currently working on a solution to control an LSC smart plug with Domoticz using MQTT protocol.
Next ESPCoreRules build will be capable of processing Domoticz MQTT messages within the rule engine. A Domoticz specific plugin is not required. Just some basic rule commands to process incoming MQTT messages and publish states on MQTT back to Domoticz.

The plug can be controlled using Domoticz, the ESP webgui or the local device button.
The updated MQTT plugin also supports the ESP Nodelist using MQTT instead of UDP messages.

A sample project demonstrates how this works:
[https://github.com/SmartNodeRules/Docum ... ticzSwitch]

tim
Normal user
Posts: 52
Joined: 05 Dec 2015, 11:24

Re: Rule engine and JSON event payload

#4 Post by tim » 01 Feb 2020, 10:01

I like to try the LSC smart plug with Domoticz MQTT
But that source is not on github yet?

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests