Text filtering to make exported JSON of RM rules human readable

The new feature in 2.2.4 to export RM4 rules as JSON is a big step forward, but the JSON is not really human readable.

Here's a quick command-line filter (very Linux specific) to take an exported JSON rule and produce a nicely formatted set of pseudo-code. This result cannot be imported into HE, but it's an easier way to share & document rules.

  1. Export the rule ("myrule.json").
  2. Filter it via:
    indent < myrule.json | sed -n -e 's/\\\\/\\/g' -e 's/<span[^>]*>//g' -e 's/<\/span>//g' -e 's/", "cond":.*/"/' -e 's/%/%%/g' -e 's/.*label": "/printf "/p' |sh

Note: that's somewhat obscure filtering, and it ends by passing the output to your shell. Do not trust my code (I wouldn't!) and closely examine what it does before blindly executing commands.

That whole mess of line noise takes myrule.json -- which is zero lines (no EOL character), 419 words and 22K characters and produces something fairly readable, like:

END-IF
IF (Garage Door Opener open(F) [FALSE]) THEN
IF (Garage Door Automatic Closing(on) is on(T) [TRUE]) THEN
ELSE
END-IF
Cancel Delayed Actions
        Garage close: Garage Door Opener --> delayed: 0:20:00 (cancelable)
IF ((   Day in Monday, Tuesday, Wednesday, Thursday, Friday(T)  AND 
        Time between 7:30 PM EST and 5:00 PM EST(F)
)  OR 
(       Day in Saturday, Sunday(F)  AND 
        Time between 9:00 PM EST and 8:00 AM EST(F)
) [FALSE]) THEN
        IF (Variable NoticePending(false) = false(T) [TRUE]) THEN
                Set NoticePending to true
                Set NoticePending to false
                Notify 1 Phone - Hubitat App, Twilio Notification - 1, Twilio Notification - 2 and Speak on 1 office Squeezeplayer (piment), Livingroom Squeezeplayer, Parlor Squeezeplayer, VIZIOCastAudio9043, Bedroom Squeezeplayer, Dining Room Squeezeplayer(volume: 100): 'Garage door left open since %time%.' --> delayed: 0:10:00 (cancelable)
        END-IF
END-IF
Set NoticePending to false
3 Likes

Is there any way to import rules INTO Rule Machine from a text-based representation? I find the construction of rules using the RM GUI to be cumbersome and unintuitive and have had to resort to coding the rules first in a text editor and then translating the logic screen-by-screen through RM's rule creation workflow.

JSONlint lets you go back and forth from compressed to human readable pretty easily too with the ?reformat=compress option. It’s just one more way to skin a cat.