红色节点:通过属性值访问XML节点

时间:2019-05-05 09:01:57

标签: node-red

我使用Node-Red查询状态列表,该状态列表返回XML格式。它基本上来自我的Homematic家庭自动化中央单元,即传感器的值和状态。

现在我正在专门查询每个传感器,但是还有一个功能可以一次获取所有内容,这对我来说更经济,因为无论如何我都需要多个值。

但是,我不知道是否可以保证这些值始终位于同一位置。

XML看起来像这样:

XML provided by central unit

在Node-Red调试窗口中,我可以看到这样的结构:

XML in Node Red

我知道如何分别访问每个节点,但是如果我使用copy Path方法:

payload.state.device[0].channel[1].datapoint[0].$.value

如果中央单元中的API将来会更改顺序(也许如果我添加另一个/删除/重新连接传感器?),那最终将崩溃。

每个Sensor都有自己的ID,您可以将其显示为

<device name="Fenster Bad" ise_id="3416" unreach="false" config_pending="false">
设备节点中的

ise_id属性。

现在的问题:

我正在尝试访问以蓝色突出显示的值。 我可以使用不同的ise_ids(高亮红色)到达那里。

XML and highlighted what I want

但是我如何告诉节点Red不要沿着索引走,而是通过ise_id属性走?

类似的东西:

payload.state.device['ise_id=3416'].channel[1].datapoint[0].$.value

但是我在网上找不到任何明确做到这一点的线程或教程...

请在此处找到示例XML:

https://pastebin.com/ic4eDXpp

1 个答案:

答案 0 :(得分:0)

可能的解决方案:

enter image description here

xml节点会将XML文件转换为JavaScript对象。下一个节点(即配置有jsonata表达式的变更节点)将仅过滤所需ise_id的数据,无论它在对象(或xml)结构中的位置如何。

您可以在以下链接中了解有关jsonata的更多信息:https://docs.jsonata.org/overview

您可能要专门阅读有关Predicate queries的页面:https://docs.jsonata.org/predicate

无论如何,我很乐意澄清任何问题。此外,您可以在Node-RED forum

中找到许多jsonata示例(或询问问题)

节点REd流:

[{"id":"d4dde310.a8375","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"968c62d.36b67a","type":"inject","z":"d4dde310.a8375","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":120,"wires":[["37118c19.2fd204"]]},{"id":"37118c19.2fd204","type":"file in","z":"d4dde310.a8375","name":"Read XML file","filename":"C:\\Users\\OCM\\.node-red\\static\\nrfiles\\s1.xml","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":340,"y":120,"wires":[["a493ccb.367ae3"]]},{"id":"76cd1e5e.a132f","type":"debug","z":"d4dde310.a8375","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"filter","targetType":"msg","x":780,"y":120,"wires":[]},{"id":"a493ccb.367ae3","type":"xml","z":"d4dde310.a8375","name":"","property":"payload","attr":"","chr":"","x":490,"y":120,"wires":[["70d00d31.6dd424","e7614ac8.183a78"]]},{"id":"70d00d31.6dd424","type":"change","z":"d4dde310.a8375","name":"","rules":[{"t":"set","p":"filter","pt":"msg","to":"payload.**[ise_id = '3418']","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":120,"wires":[["76cd1e5e.a132f"]]},{"id":"e7614ac8.183a78","type":"debug","z":"d4dde310.a8375","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":200,"wires":[]}]

过滤后的ise_id ='3418'的输出(例如):

enter image description here