# Compare different msg and Trigger

**URL:** <https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729>\
**Category:** General\
**Created:** [5 December 2021 07:51 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729 "2021-12-05T07:51:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herbert](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Herbert](https://discourse.nodered.org/u/Herbert)\
**Post date:** [5 December 2021 07:51 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729/1 "2021-12-05T07:51:46Z")

</div>

Hello,

I tried to compare two values from my solar inverter.  
One value is the state of charge from my battery: msg.payload  
Other value should be the goal. Name of msg is: msg.payload.toSolar with value 95.6. This value is a fix value, changing each day/month.

If the solar power charge my battery the msg.payload increase and increase. If the msg.payload is equal or bigger than the value msg.payload.toSolar then an Trigger should be started.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/136e38fd464f6ac332e406ece669f0fc3515e6a7.png)

```auto
[{"id":"35415e42.1457a2","type":"link in","z":"1bd00a5b.7e9796","name":"toSolar Wert","links":["5aa76440.263a4c"],"x":75,"y":2420,"wires":[["d6769cef.21438","ebdf752.9b79d88"]]},{"id":"85d3e269.3d413","type":"switch","z":"1bd00a5b.7e9796","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"payload.toSolar","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":2440,"wires":[["7c61d8f2.904908","30b30da2.cc3a82"]]},{"id":"c1d898f1.d7c888","type":"link in","z":"1bd00a5b.7e9796","name":"","links":["ef1b6ec4.06e55"],"x":75,"y":2480,"wires":[["1e621e7f.ce4382","d98a81cc.e0603"]]},{"id":"7c61d8f2.904908","type":"trigger","z":"1bd00a5b.7e9796","name":"","op1":"stop","op2":"","op1type":"str","op2type":"nul","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":600,"y":2420,"wires":[["d08829ba.642a58"]]},{"id":"30b30da2.cc3a82","type":"trigger","z":"1bd00a5b.7e9796","name":"","op1":"","op2":"3","op1type":"nul","op2type":"str","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":600,"y":2460,"wires":[["ad9b3f33.0303"]]},{"id":"d6769cef.21438","type":"debug","z":"1bd00a5b.7e9796","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.toSolar","targetType":"msg","statusVal":"payload","statusType":"auto","x":240,"y":2340,"wires":[]},{"id":"1e621e7f.ce4382","type":"debug","z":"1bd00a5b.7e9796","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":190,"y":2540,"wires":[]},{"id":"ebdf752.9b79d88","type":"function","z":"1bd00a5b.7e9796","name":"string to number","func":"msg.payload = Number(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":200,"y":2420,"wires":[["85d3e269.3d413"]]},{"id":"d98a81cc.e0603","type":"function","z":"1bd00a5b.7e9796","name":"string to number","func":"msg.payload = Number(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":200,"y":2480,"wires":[["85d3e269.3d413"]]}]

```

I tried it but it doesn´t work. Can somebody help me?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 December 2021 09:05 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729/2 "2021-12-05T09:05:16Z")

</div>

You can't sensibly compare msg.payload with msg.payload.toSolar.

Are the two values which you want to compare in the same message eg  
**msg.payload = ("now": 55, "target": 100};**?  
if so your switch node can compare msg.payload.now with msg.payload.target

If not, store the target in context:

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e090b9cf30f667b76fec9e38a5aa06d2d42020d3.jpeg)

---

<div class="post-metadata">

**Author:** ![Herbert](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Herbert](https://discourse.nodered.org/u/Herbert)\
**Post date:** [5 December 2021 09:22 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729/3 "2021-12-05T09:22:54Z")

</div>

Hi jbudd,

hmm I´m not an expert...  
The "now" value comes from a external device and is available on msg.payload.

The target comes from a cronplus node.

I don´t understand what you mean with target in context with your example and absolutely not how I can program it.

Here is my target flow:

```auto
[{"id":"c2330005.6fdad","type":"cronplus","z":"28283065.a2357","name":"December","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"schedule1","topic":"december","payloadType":"json","payload":"{\"toGrid\":96.0,\"toSolar\":98.0}","expressionType":"cron","expression":"0 */1 * * DEC *","location":"","offset":"0","solarType":"selected","solarEvents":"sunrise,sunset"}],"x":550,"y":1480,"wires":[["3b51f18b.c111ee"]]},{"id":"3b51f18b.c111ee","type":"change","z":"28283065.a2357","name":"update flow.limits","rules":[{"t":"set","p":"limits","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":1140,"wires":[["eb349c18.c008f","d70c70cb.f367b","21f0bc00.0a5b14","70b77b2f.9b6574","fe1af563.2ef258","4f8dff19.b29fa"]]},{"id":"eb349c18.c008f","type":"debug","z":"28283065.a2357","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":870,"y":1180,"wires":[]},{"id":"fe1af563.2ef258","type":"show-value","z":"28283065.a2357","name":"toSolar","path":"toSolar","x":1140,"y":1200,"wires":[["1e00a80b.db26c8"]]},{"id":"1e00a80b.db26c8","type":"calculator","z":"28283065.a2357","name":"Minus 0.1","inputMsgField":"payload.toSolar","outputMsgField":"payload.toSolar","operation":"sub","constant":"2.4","round":true,"decimals":"2","x":1300,"y":1200,"wires":[["a33735ef.fa8af8","5aa76440.263a4c"]]},{"id":"a33735ef.fa8af8","type":"debug","z":"28283065.a2357","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.toSolar","targetType":"msg","statusVal":"payload","statusType":"auto","x":1500,"y":1220,"wires":[]}]

```

My data are available as your example like inject number value.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 December 2021 09:35 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729/4 "2021-12-05T09:35:53Z")

</div>

If they are in different mesages then you will need to store one in context or join the messages  
here is an example of joining messages and comparing

```auto
[{"id":"3243f1f3.f1c8b6","type":"inject","z":"88fadd65b9040724","name":"solar","props":[{"p":"payload.toSolar","v":"95.6","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":90,"y":580,"wires":[["ebdf752.9b79d88"]]},{"id":"ebdf752.9b79d88","type":"function","z":"88fadd65b9040724","name":"string to number","func":"msg.payload = Number(msg.payload.toSolar)\nmsg.topic = \"toSolar\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":580,"wires":[["d73e4f7.a9d763"]]},{"id":"d73e4f7.a9d763","type":"join","z":"88fadd65b9040724","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":600,"wires":[["85d3e269.3d413"]]},{"id":"85d3e269.3d413","type":"switch","z":"88fadd65b9040724","name":"","property":"payload.charge","propertyType":"msg","rules":[{"t":"gte","v":"payload.toSolar","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":660,"y":600,"wires":[["7c61d8f2.904908","30b30da2.cc3a82"]]},{"id":"d98a81cc.e0603","type":"function","z":"88fadd65b9040724","name":"string to number","func":"msg.payload = Number(msg.payload)\nmsg.topic = \"charge\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":640,"wires":[["d73e4f7.a9d763"]]},{"id":"7c61d8f2.904908","type":"trigger","z":"88fadd65b9040724","name":"","op1":"stop","op2":"","op1type":"str","op2type":"nul","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":670,"y":540,"wires":[["1e621e7f.ce4382"]]},{"id":"30b30da2.cc3a82","type":"trigger","z":"88fadd65b9040724","name":"","op1":"","op2":"3","op1type":"nul","op2type":"str","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":650,"y":660,"wires":[["1e621e7f.ce4382"]]},{"id":"b060fa5b.23b808","type":"inject","z":"88fadd65b9040724","name":"charge","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"96","payloadType":"num","x":80,"y":640,"wires":[["d98a81cc.e0603"]]},{"id":"1e621e7f.ce4382","type":"debug","z":"88fadd65b9040724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":720,"wires":[]}]

```

hope it helps.

---

<div class="post-metadata">

**Author:** ![Herbert](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Herbert](https://discourse.nodered.org/u/Herbert)\
**Post date:** [5 December 2021 09:56 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729/5 "2021-12-05T09:56:07Z")

</div>

Hello,

THANKS it works. I love this Forum!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 December 2021 09:56 UTC](https://discourse.nodered.org/t/compare-different-msg-and-trigger/54729/6 "2021-12-19T09:56:25Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
