Repost State of Mqtt Topic if other Topic's state is 1

Hi,
I've run into a problem: I'd link to have a trigger on an MQTT topic(for ex. TopicY's state is 1 ) that repeats a current topic (TopicX(State: 3)) to topicX like this: https://imgur.com/a/RtuGcbJ
It is for an AV-Receiver with an Ir Remote build myself. The receiver only wakes up when I switch Channels which sends an Ir command with the Channel I'd like to select. I tried understanding another post, but since I'm a Newbee I can't quite rewrite the code myself. Here is the Thread: Simple repeat last msg node to re-send MQTT
Thanks a lot for any Help :slight_smile:

For future reference you should be able to paste an image directly in here.
Firstly you need to get both of the inputs together in one message. To do that you can use a Join node in key/value pair mode. See this post in the cookbook for how to join the two streams. Have a go at that (start by downloading the example there to see how it works). You will need And Every Subsequent Message ticked in the Join node, I am not sure if that is included in the example or not. You can use the topic coming out of the join node to determine which input has triggered and then setup the message to send to the MQTT Out node.

1 Like

Thanks a lot for your answer. I'm not quite sure how I should do the last part of your answer:

You can use the topic coming out of the join node to determine which input has triggered and then set up the message to send to the MQTT Out node.

How should I do this? Do you have any ideas?

Have you got the first bit going? Feed the output of the Join node into a debug node set to show complete message and see what you get out.
You can use a Switch node to only pass through messages out of the Join that have the TopicY topic.

Yes, the join node works flawless, but I'm not quite sure how I can switch the topics so that If topic 1 is high that topic 2 is resend

I realise that for a simple solution you will need two switch nodes (they can probably be combined but get it working with two first) Configure the first to pass on the message to output 1 if the topic is TopicY. That is all you need configure as it means that any other message will be dropped. Then send that to another one that only passes it on if the value of TopicY in the payload is 1. Now you know that if a message is passed on then you need to publish the mqtt out, so use a Change node to setup the topic and payload appropriately and pass it to the MQTT node.

I tried following your instructions as closely as I could, but after the second switch node, nothing gets recognized. Any Ideas why?

What are you switching on in the second one? It should actually msg.payload.topicy (or whatever the topic actually is). My previous post seems to have had a mistake that I assume I subconsciously made in order to see if you were paying attention :slight_smile:

If still no joy then post the flow after reading

In the second switch you need to switch on msg.payload.Sportraum/Av_turnoff == 1 but that isn't valid javascript syntax because it thinks the / is a divide symbol. So in the Switch node you have to use msg.payload["Sportraum/Av_turnoff"]

I tried uploading the script, but it needs to be checked, so I try showing you what I did with Pictures:


Firstly I tried this config for switch 1 and this config for switch 2:

and this as a whole:

but all of this effort does nothing.
After your message I've as well tied this:
Conf1:

Conf2:

All:

But the last config did not return anything at all. I'm very sorry to steal your time that way. I hope you can help me :crossed_fingers:. Thanks a lot again.

Try Nr.2 Posting the code:

[{"id":"a2922961.12cbf8","type":"tab","label":"Sportraum 2","disabled":false,"info":""},{"id":"77627e6e.f1c15","type":"mqtt in","z":"a2922961.12cbf8","name":"If Sportraum/Av_turnoff = 1","topic":"Sportraum/Av_turnoff","qos":"0","datatype":"auto","broker":"7f43c1db.c7b8b","x":210,"y":240,"wires":[["a7166a66.6ab018"]]},{"id":"e37cbd7c.725c1","type":"mqtt in","z":"a2922961.12cbf8","name":"Sportraum/Av_input Topic to resend","topic":"Sportraum/Av_input","qos":"0","datatype":"auto","broker":"7f43c1db.c7b8b","x":180,"y":320,"wires":[["a7166a66.6ab018"]]},{"id":"a7166a66.6ab018","type":"join","z":"a2922961.12cbf8","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":410,"y":260,"wires":[["f23daef9.1fac7"]]},{"id":"29a376dc.69643a","type":"debug","z":"a2922961.12cbf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"Sportraum/Av_turnoff","targetType":"msg","x":810,"y":200,"wires":[]},{"id":"f23daef9.1fac7","type":"switch","z":"a2922961.12cbf8","name":"switch msg.Sportraum/Av_turnoff","property":"Sportraum/Av_turnoff","propertyType":"msg","rules":[{"t":"eq","v":"Sportraum/Av_turnoff","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":320,"wires":[["29a376dc.69643a","92d6c292.fe17c"]]},{"id":"92d6c292.fe17c","type":"switch","z":"a2922961.12cbf8","name":"switch msg.payload[\"Sportraum/Av_turnoff\"]","property":"msg.payload[\"Sportraum/Av_turnoff\"]","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":870,"y":380,"wires":[["2c12c89a.c71678"],["2c12c89a.c71678"]]},{"id":"2c12c89a.c71678","type":"debug","z":"a2922961.12cbf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":950,"y":280,"wires":[]},{"id":"7f43c1db.c7b8b","type":"mqtt-broker","z":"","name":"192.168.178.15","broker":"192.168.178.15","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

In the second one you have msg.msg.payload["..."]. Should be msg.payload[".."]
It puts the first msg in for you.

1 Like

One mistake, changing did sadly not help. Maybe the join node is wrong:


Is this correct? It worked and pasted out both values to the msg.payload debug node.

Look in a debug node showing the output of the Join node and see what type the value of the trigger (0 or 1) is. Is it a string (it will be be shown with double quotes round it) or a number? In the second Switch you are testing for it to be a string value, but I suspect it should be a number (select 0/9 in the type dropdown).


That's the output of the debug node. So it is not a string? What else is it when it is not a string?

It is in quotes so it is a string, not at PC at the moment, will have a look a bit later.

You had the first switch wrong too, it was supposed to be checking what the topic was. This is working for me. I have replaced the MQTT In with inject nodes for testing and added a Change node to set the topic and payload ready to send to the MQTT Out node.
Once you are happy that all is working properly you can remove the output 2 test in the second node which was just for checking it is working.

[{"id":"2af783ff.e1ec6c","type":"debug","z":"d5c4fed6.cd462","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"Sportraum/Av_turnoff","targetType":"msg","x":810,"y":200,"wires":[]},{"id":"e904c3b3.9ee6d","type":"debug","z":"d5c4fed6.cd462","name":"OP 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":814,"y":436,"wires":[]},{"id":"43f567b3.9a1218","type":"debug","z":"d5c4fed6.cd462","name":"OP 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":868,"y":290,"wires":[]},{"id":"222f68fb.5674c8","type":"comment","z":"d5c4fed6.cd462","name":"MQTT Out here","info":"","x":825,"y":478,"wires":[]},{"id":"88708445.b9f158","type":"change","z":"d5c4fed6.cd462","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"Sportraum/Av_input","tot":"str"},{"t":"move","p":"payload[\"Sportraum/Av_input\"]","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":601,"y":438,"wires":[["e904c3b3.9ee6d"]]},{"id":"d1fe14e8.baa538","type":"switch","z":"d5c4fed6.cd462","name":"switch msg.payload[\"Sportraum/Av_turnoff\"]","property":"payload[\"Sportraum/Av_turnoff\"]","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":642,"y":353,"wires":[["88708445.b9f158"],["43f567b3.9a1218"]]},{"id":"fd4f5815.711428","type":"switch","z":"d5c4fed6.cd462","name":"switch msg.Sportraum/Av_turnoff","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Sportraum/Av_turnoff","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":575,"y":276,"wires":[["2af783ff.e1ec6c","d1fe14e8.baa538"]]},{"id":"8fec44bf.38cd88","type":"join","z":"d5c4fed6.cd462","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":355,"y":275,"wires":[["fd4f5815.711428"]]},{"id":"6a81500e.aff508","type":"inject","z":"d5c4fed6.cd462","name":"","topic":"Sportraum/Av_input","payload":"Some Message","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":184,"y":382,"wires":[["8fec44bf.38cd88"]]},{"id":"d1690923.b09c","type":"inject","z":"d5c4fed6.cd462","name":"","topic":"Sportraum/Av_turnoff","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":151,"y":296,"wires":[["8fec44bf.38cd88"]]},{"id":"633b02b0.9c363c","type":"inject","z":"d5c4fed6.cd462","name":"","topic":"Sportraum/Av_turnoff","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":152,"y":259,"wires":[["8fec44bf.38cd88"]]}]
1 Like

Thank you for all of your help. It finally works :smile:. This is the final code that I use if somebody else wants to use this flow in the future. I had to change a bit to work with my arduino Ir remote(on my github) but now it works:

[{"id":"a2922961.12cbf8","type":"tab","label":"Sportraum 2","disabled":false,"info":""},{"id":"77627e6e.f1c15","type":"mqtt in","z":"a2922961.12cbf8","name":"If Sportraum/Av_turnoff = 1","topic":"Sportraum/Av_turnoff","qos":"0","datatype":"auto","broker":"7f43c1db.c7b8b","x":250,"y":200,"wires":[["166bc5af.1e762a"]]},{"id":"e37cbd7c.725c1","type":"mqtt in","z":"a2922961.12cbf8","name":"Sportraum/Av_input Topic to resend","topic":"Sportraum/Av_input","qos":"0","datatype":"auto","broker":"7f43c1db.c7b8b","x":220,"y":280,"wires":[["166bc5af.1e762a"]]},{"id":"cc98b6ac.188d58","type":"debug","z":"a2922961.12cbf8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"Sportraum/Av_turnoff","targetType":"msg","x":950,"y":140,"wires":[]},{"id":"6e1263c8.61fcbc","type":"debug","z":"a2922961.12cbf8","name":"OP 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1010,"y":340,"wires":[]},{"id":"97d8506b.0dd06","type":"debug","z":"a2922961.12cbf8","name":"OP 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1008,"y":230,"wires":[]},{"id":"6e5d5fc3.bc2db","type":"change","z":"a2922961.12cbf8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"Sportraum/Av_input","tot":"str"},{"t":"move","p":"payload[\"Sportraum/Av_input\"]","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":741,"y":378,"wires":[["6e1263c8.61fcbc","b07fb8c3.a75848","5a7f217e.7bdcc"]]},{"id":"3008ca8d.5b3e96","type":"switch","z":"a2922961.12cbf8","name":"switch msg.payload[\"Sportraum/Av_turnoff\"]","property":"payload[\"Sportraum/Av_turnoff\"]","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":782,"y":293,"wires":[["6e5d5fc3.bc2db"],["97d8506b.0dd06"]]},{"id":"f1791f5e.67d14","type":"switch","z":"a2922961.12cbf8","name":"switch msg.Sportraum/Av_turnoff","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Sportraum/Av_turnoff","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":715,"y":216,"wires":[["cc98b6ac.188d58","3008ca8d.5b3e96"]]},{"id":"166bc5af.1e762a","type":"join","z":"a2922961.12cbf8","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":495,"y":215,"wires":[["f1791f5e.67d14"]]},{"id":"faa7aba5.be5718","type":"mqtt out","z":"a2922961.12cbf8","name":"","topic":"Sportraum/Av_input","qos":"","retain":"","broker":"7f43c1db.c7b8b","x":1150,"y":420,"wires":[]},{"id":"5a7f217e.7bdcc","type":"delay","z":"a2922961.12cbf8","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":940,"y":440,"wires":[["faa7aba5.be5718"]]},{"id":"b07fb8c3.a75848","type":"trigger","z":"a2922961.12cbf8","op1":"0","op2":"","op1type":"str","op2type":"nul","duration":"250","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":960,"y":380,"wires":[["faa7aba5.be5718"]]},{"id":"7f43c1db.c7b8b","type":"mqtt-broker","z":"","name":"192.168.178.15","broker":"192.168.178.15","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Excellent.
The challenge now (if you choose to accept it) is to work out how to combine the two switch nodes. You have to use a JSONata expression in the Property field and then switch based on the value of the expression.
On the other hand if you just want to get on and get other more interesting things going then I shouldn't worry, what you have is completely acceptable. I haven't worked out the solution myself so I can't just post it.

Never change a running system!

--My Dad
:smile: