@jacke sagte in Mqtt Wert +/- in 2 Werte Schreiben:
@mickym
ja ist kein NodeRed funktioniert hat es jetzt möchte ich es trotzdem über NodeRed noch schaffen.
$abs(payload) konvertiert denn negativen Wert in einen positiven habe es gerade getestet hat geklappt. Aber ich muss den Wert der negativ und positiv ist in 2 Datenpunkte schreiben
wenn negativ in DP1 als positiven Wert (abs(payload) und DP2 dann 0
wenn positiv DP1 = 0 und DP2 = wert (payload)
Nun dann ist das ja keine mathematische Funktion, sondern Du willst 2 Fälle unterscheiden und in unterschiedliche Datenpunkte dann entweder die payload direkt oder den invertierten Wert schreiben. Klassisch wie mit dem falls in Deinem Blockly untersuchst Du unterschiedliche Fälle mit einer Switch Node.
42c70881-ca5a-4f21-b056-c7e0ff5a2d8c-image.png
Hier zum Import:
Spoiler
[{"id":"bd5f7a9a53ef5678","type":"inject","z":"289f539dcc33814e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-45","payloadType":"num","x":190,"y":7200,"wires":[["e161cbdca92e3b02"]]},{"id":"1f315bca40a9dd59","type":"inject","z":"289f539dcc33814e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"45","payloadType":"num","x":190,"y":7240,"wires":[["e161cbdca92e3b02"]]},{"id":"e161cbdca92e3b02","type":"switch","z":"289f539dcc33814e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":7220,"wires":[["7b96cde06b278c06","1b68ac68575bd4f9"],["0b61b1c6cc96e19b","771afa5b951f038d"]]},{"id":"0b61b1c6cc96e19b","type":"change","z":"289f539dcc33814e","name":"payload *-1","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload * -1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":7180,"wires":[["205119c4b27273ce"]]},{"id":"97d10ea79b2fc663","type":"ioBroker out","z":"289f539dcc33814e","d":true,"name":"","topic":"DP1","ack":"true","autoCreate":"false","stateName":"","role":"","payloadType":"","readonly":"","stateUnit":"","stateMin":"","stateMax":"","x":790,"y":7160,"wires":[]},{"id":"771afa5b951f038d","type":"change","z":"289f539dcc33814e","name":"payload = 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":7260,"wires":[["1b68ac68575bd4f9"]]},{"id":"0fc9b8afd7f05c1b","type":"ioBroker out","z":"289f539dcc33814e","d":true,"name":"","topic":"DP2","ack":"true","autoCreate":"false","stateName":"","role":"","payloadType":"","readonly":"","stateUnit":"","stateMin":"","stateMax":"","x":790,"y":7240,"wires":[]},{"id":"7b96cde06b278c06","type":"change","z":"289f539dcc33814e","name":"payload = 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":7140,"wires":[["205119c4b27273ce"]]},{"id":"72de10b2d51a21b5","type":"debug","z":"289f539dcc33814e","name":"Check DP1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":7100,"wires":[]},{"id":"b219337d3d941aaa","type":"debug","z":"289f539dcc33814e","name":"Check DP2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":7300,"wires":[]},{"id":"205119c4b27273ce","type":"junction","z":"289f539dcc33814e","x":680,"y":7160,"wires":[["97d10ea79b2fc663","72de10b2d51a21b5"]]},{"id":"1b68ac68575bd4f9","type":"junction","z":"289f539dcc33814e","x":680,"y":7220,"wires":[["0fc9b8afd7f05c1b","b219337d3d941aaa"]]}]
Man kann das Ganze aber auch in eine Change Node packen. 😉 Da Du die topics in der iobroker out dynamisch setzen kannst.
So hier noch um die Kurzversion ergänzt:
7292857e-41d3-4e04-a254-52ac25309e11-image.png
Auch hier wieder zum Import:
Spoiler
[{"id":"4c9de38276510bfd","type":"inject","z":"289f539dcc33814e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-45","payloadType":"num","x":190,"y":7480,"wires":[["70bcb454e20d9db9"]]},{"id":"3038fa2a5c632695","type":"inject","z":"289f539dcc33814e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"45","payloadType":"num","x":190,"y":7520,"wires":[["70bcb454e20d9db9"]]},{"id":"f7abecce71583f6e","type":"change","z":"289f539dcc33814e","name":"payload = 0","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload < 0 ? \"DP2\" :\"DP1\"","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":7480,"wires":[["513a2df25efaa762","78a2d57ff987a5d6"]]},{"id":"c6201493abe23e68","type":"change","z":"289f539dcc33814e","name":"$abs(payload)","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload < 0 ? \"DP1\" :\"DP2\"","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$abs(payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":7520,"wires":[["513a2df25efaa762","78a2d57ff987a5d6"]]},{"id":"78a2d57ff987a5d6","type":"debug","z":"289f539dcc33814e","name":"Ergebnis","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":7480,"wires":[]},{"id":"513a2df25efaa762","type":"ioBroker out","z":"289f539dcc33814e","d":true,"name":"beide Datenpunkte","topic":"","ack":"true","autoCreate":"false","stateName":"","role":"","payloadType":"","readonly":"","stateUnit":"","stateMin":"","stateMax":"","x":630,"y":7520,"wires":[]},{"id":"70bcb454e20d9db9","type":"junction","z":"289f539dcc33814e","x":280,"y":7500,"wires":[["f7abecce71583f6e","c6201493abe23e68"]]}]
Gleiche Farben bedeuten identische Ausgaben, sowohl mit dem Flow 1 als auch Flow 2.
In der ersten Change Node wird also 0 in einen Datenpunkt geschrieben, der mit topic spezifiziert wird.
b29ad752-bcb9-42e6-8b18-4688526e968c-image.png
Wenn payload <0 dann wird in eine payload mit dem Wert 0 in den Datenpunkt 2 (=topic) geschrieben, wenn die payload >=0 ist dann in Datenpunkt1 (=topic)
Umgekehrt
6fec2ec4-05d4-445c-a7f4-3c22df4aab80-image.png
war die payload > 0 dann wird der Absolutwert in Datenpunkt 1, sonst in Datenpunkt 2 geschrieben.
5c0af8e4-e830-46fe-9cc0-6433d6b3a50c-image.png
Wenn man in der iobroker-Out Node kein topic einträgt, wird das topic automatisch vom Nachrichtenobjekt genommen. Du musst also
DP1 noch gegen Deinen Pfad der Datenpunkte ersetzen, also "0_userdata.0.DP1" bzw.
DP2 gegen "0_userdata.0.DP2" - also wohin Du die beiden Werte auch immer Du schreiben willst.