I'm missing something with the inject node and the { } optoin

#1

I am trying to get to do things better.

Recent events caused me to need an injection.

Easy enough, but I am not getting how to do it with as few nods as possible.

Here is the example:

[{"id":"70154d57.8eff44","type":"inject","z":"fb549e2f.4d026","name":"Button colour setter","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"10","x":1510,"y":400,"wires":[["83970f27.82ba9"]]},{"id":"83970f27.82ba9","type":"function","z":"fb549e2f.4d026","name":"Set button colours","func":"msg.background = \"red\";\nreturn msg;","outputs":1,"noerr":0,"x":1540,"y":440,"wires":[["ca64e146.cdce28","c7de0f3c.4a4f48"]]}]

Now, why cant I put that in the injection node?

It would be in the { } part. But my efforts to do that don’t get anywhere and all I have is the red line around the field.

Thanks in advance.

0 Likes

#2

The injection node injects something in msg.payload, not in msg.

You can use JSON to inject an object like {"background":"red"}in msg.payloadbut you cannot set msg.backgroundto redwith an injection node.

0 Likes

#3

Thanks. But I think the message was lost in translation.

Say I have a button. I set its colour (background) from a message (package).
The name of the colour is derived from msg.background

I want to test it and so use an INJECT node.

I want to send a message (package) that will have a part called background and it will be the colour I want.

Trying to put it as best I know:

{background:“red”}

Then: the button gets that message and sets the background to red.

Short of adding a FUNCTION node and putting a line like:

msg.background = “red”;
return msg;

That seems … stoopid given NR’s flexibility.

I just seem to not be getting how to make that “package” up in the inject node.

Clearer?

0 Likes

#4

The Inject node only allows you to set msg.payload and msg.topic. That’s it. Nothing else.

If you need to set other message properties, then use a Change node after the inject node to set all the properties you want.

Nick

0 Likes

#5

Yeah, thanks.

B...U.....T.....

This link:


(I don't like "mixing up sources" but..... )

He goes on to say:

Let’s take an example. Let’s say that by pressing the test button we want to set a needle value to 10 AND we want to send the message “hello” to an LCD display in our gauge.

Then goes on to talk about how it can do all this magic stuff.
It is just a case of ...... not sticking with the "default" settings.

Not a string, but an object - but how do you make use of this? Simple. In your template – or function or whatever you are firing this message into:

Then maybe it is an OBJECT (I don't know all the terms).

It is just he talks about all this stuff, but the included pictures are ..... woeful when you are trying to read what is happening.

And as it isn't fully explained..... I just fall in a heap.

Sure, I should maybe talk to him about it.
But I am trying to learn.
There are a lot of people here who could maybe help me - as well.

So reading what you said: I have the whole thing wrong? The inject node can't create a message packet/thingy with things like: background and have it set to "red" - for instance.

Because (again from the other site) it is implied it can.

As soon as I type ANYTHING in that line, the line goes RED and errors.

I'm still stuck and confused.

This is just to help me poke around and be able to easily (now I think a bad choice) inject things where I need them.

I shall have to stick to adding a function block between the two. :frowning:

Just for (I hope) 100% clarity:

[{"id":"34f8a530.d178ea","type":"inject","z":"3f235824.9f514","name":"","topic":"","payload":"blah","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":80,"wires":[["3f75286f.27d398"]]},{"id":"5eee3a23.9fe554","type":"debug","z":"3f235824.9f514","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":530,"y":80,"wires":[]},{"id":"3f75286f.27d398","type":"function","z":"3f235824.9f514","name":"","func":"msg.background = \"red\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":80,"wires":[["5eee3a23.9fe554"]]}]
6/17/2018, 4:24:02 PMnode: 5eee3a23.9fe554
msg : Object
object
_msgid: "9876aafa.02a2a8"
topic: ""
payload: "blah"
background: "red"

Can that be done WITHOUT the function block between the two nodes?
Just the inject node and the debug nodes.

0 Likes

#6

The screenshot you show of the Inject node shows it setting msg.payload to an object.

The message it injects will have the properties msg.payload.needle and msg.payload.lcd.

I answered that exact question in my reply.

0 Likes

#7

Again: Thanks.

It isn’t I am wanting to be argumentative.

I guess it is just the frustration is getting a bit high. 7 hours key bashing can get to people.

Thanks.

I’ll just use the function node as I have been doing and it works.
Though you said a CHANGE node… I’ll maybe look into that. I don’t remember seeing it listed.
But that’s probably just an oversight at my end.

0 Likes

#8

You really would save yourself a lot of time if you researched about javascript objects and properties for a few hours so that you understand the concept of object properties such as msg.payload, msg.topic, msg.background, msg.payload.background. You are currently wasting a lot of your time making the same basic errors time after time as you have not fully taught yourself the fundamentals of data in javascript.

1 Like

#9

100% agree.

However, there is a bit of undeclared “stupidity” on my part.

I can learn. I’ve done a lot of it.

But I am finding “modern instructions” written more for people who already know the answer, rather than for someone who doesn’t know about it.

So I am trying to learn, and yes I keep repeating mistakes. Probably because I haven’t studied them enough and also because I am still not … confident (?) enough to really (REALLY) know how they work. It is more a case of that’s how they said it, I’ll try it.

Like for example how I got confused with the msg.payload.(other stuff) from the other site.

As hard as I looked I didn’t see that it WAS (IS?) msg.payload.needle rather than msg.needle
and so I thought I could skip the function node (or change node) altogether.

I am appreciative that you (and others) take the time to show me where I am making mistakes.
I don’t want to (or even like the idea of) wasting people’s time.

I guess I am stuck between a rock and hard place wanting to learn.

So I don’t know if it is me - being stupid - or that the instructions are badly written, and making my life misery.

0 Likes

#10

It’s a fundamental problem when trying to learn be searching the internet that is is difficult to find instructions that are aimed at the right level. Learning javascript from the internet is also a problem because most of the tutorials assume you are writing js in a web page. I don’t know whether anyone here can recommend a basic level tutorial for writing code such as we use in node-red.

0 Likes

#11

Remember you can pass messages to the Debug node and it will show your the structure of the message. If you are ever in doubt, use the Debug node to ‘see’ the message structure.

0 Likes

#12

This will set msg.payload.background to “red”

[{"id":"d8d8d5d3.b88fb","type":"inject","z":"4982e5e6.1eab9c","name":"","topic":"","payload":"{\"background\":\"red\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":400,"wires":[["ff76b95.c4f1248"]]},{"id":"ff76b95.c4f1248","type":"debug","z":"4982e5e6.1eab9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":380,"y":400,"wires":[]}]

Your button could check that and change colour?

1 Like

#13

Now that is weird.

When I went to do what you just said - and of course it works - I saw a red rectangle around the field - see attached.

So I thought it meant that wasn't valid.

But now, I see that if I press the DONE button and reopen it, the node is happy.

0 Likes

#14

I don’t think the colour of the box is a validation indication, I think it just means you’re editing that property - it has focus

0 Likes

#15

No, the red border is a validation failure. However it only revalidates when the input loses focus (ie you click away from it). It doesn’t revalidate with every key press. I’m currently making some changes to the TypedInput widget and will look at improving the real-time validation to give better feedback as you type.

0 Likes

#16

Thanks for that - I’ve ignored the colour and just pressed ‘done’ to see if I’ve made a mistake

0 Likes

#17

Actually, the red box does indicate an invalid JSON string – when it is valid, the box focus is light grey. The problem is that the validation only occurs after focus leaves the field (so clicking outside the box, or using tab to go to the next field…

However, there is a (imo) better input mode available, using the built-in multiline ACE editor. You activate that by clicking the little ellipsis button ... to the right of the input text field. It validates your JSON string after every keystroke, shows the error box on the line where a problem was detected, and even has a “Format” button to properly indent and space your string – very nice!

0 Likes

#18

I don’t understand why the box is red when the input is valid

0 Likes

#19

@asandford If when you open the dialog the value is invalid it will have a red border. The code will not do anything to recheck until the input loses focus - ie you tab or click away from the input. So you may type something valid in, but it will still show red until you click somewhere to trigger it to revalidate the value.

0 Likes

#20

(New day here. Just got up.)

Again thanks to all.

That will have to be put down to a few things not being understood.

With recent things shown to me, I am just messing around to play with what I was shown and try to get a grip on it.

Alas the gremlins are out and about because it doesn’t seem to be “working”.

Given asandford’s post, I am trying it to see if it works.
(Not saying what you said is wrong, but more pointing out what is happening HERE, NOW.)

This is what I am playing with just now.
A slight growth on what was shown:

[{"id":"caa513f6.edf078","type":"ui_button","z":"af7ff65f.3f93e","name":"","group":"3ccb7302.e512c4","order":0,"width":0,"height":0,"passthru":false,"label":"button","color":"","bgcolor":"","icon":"","payload":"{\"payload\":\"\",\"background\":\"red\"}","payloadType":"json","topic":"","x":190,"y":320,"wires":[["b4fdff23.a01ee"]]},{"id":"b4fdff23.a01ee","type":"ui_button","z":"af7ff65f.3f93e","name":"","group":"1b182f8e.fc089","order":0,"width":0,"height":0,"passthru":true,"label":"victim","color":"","bgcolor":"{{msg.background}}","icon":"","payload":"","payloadType":"str","topic":"","x":570,"y":320,"wires":[["528e7285.5ae87c"]]},{"id":"af9f506c.8da9b8","type":"inject","z":"af7ff65f.3f93e","name":"","topic":"","payload":"{\"payload\":\"\",\"background\":\"red\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":240,"wires":[["ebb88f26.c21e2","b4fdff23.a01ee"]]},{"id":"ebb88f26.c21e2","type":"debug","z":"af7ff65f.3f93e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":400,"y":240,"wires":[]},{"id":"528e7285.5ae87c","type":"debug","z":"af7ff65f.3f93e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":320,"wires":[]},{"id":"3ccb7302.e512c4","type":"ui_group","name":"Group 1","tab":"5c833f2.04f73c","order":1,"disp":true,"width":6},{"id":"1b182f8e.fc089","type":"ui_group","name":"Group 2","tab":"5c833f2.04f73c","order":2,"disp":true,"width":6},{"id":"5c833f2.04f73c","type":"ui_tab","z":"","name":"Colour test","icon":"dashboard","order":5}]

Originally it was the INJECT and DEBUG nodes.
It is understood by me that if I send a … package (?) to a button with msg.background set to a valid value and in the button have the background colour option set to {{msg.background}} when the package arrives the background colour will change to said colour.

This has been shown to work with my recent post with the two buttons and switch, with the button changing colour as well as the switch indicating the “valid time” to press the second button.

So that a given to work.

I wanted to test it again with two buttons. The first simply sends the msg.background package to the second button and it changes colour.

That will establish a “base line” on which I can build. - if you get my meaning.

It didn’t work.
I added the original INJECT node. Didn’t work.

Looked at my “working” code and msg.payload is also “set” to “”.

Added that to the { } part and tried.

Still doesn’t work.

So, again I am not seeing the elephant in the room.

Sorry, but this may be a fundamental flaw in how I see things and how I think.
If I don’t learn/understand/change that, I am not going to get anywhere.

0 Likes