Single Imp receiving multiple messages


I am having a bit of difficulty with receiving agent messages from multiple imps to a single Imp. Let me elaborate:

I have three imps in total. Each imp is sending a specific integer to each imp, which is well. Illustrated below is my example:

`function Main(value) { //value = incoming values from imps

if(value==1){ //imp 1 is sending a 1 through agent and received here to 3rd imp
if(value==0){ //nothing across the board
if(value==2){  //imp 2 is sending a 2 through agent and received here to 3rd imp


My issue is when I try to read two imps simultaneously, it appears in the monitor that the values are not continuously updating, which activates the 3rd imp based on which of the other two imps was activated first. Here is how I tried to implement this step without much luck:

` if(value==2 && value==1){ //imp 1+2 is sending a integers simultaneously and received here to 3rd imp
server.log(“both imps!”);


Errrrm, a variable can only ever be one value at a time. Doing if (value==2 && value==1) is essentially doing if (false) - both conditions can NEVER be true concurrently. Your function is being called at different times with value==2 and value==1.

If this concept is a bit alien, I suggest you read some programming books…

To keep this simple, how about imp 1 sends messages in the range 10-19, imp 2 sends messages in the range 20-29 and imp 3 sends messages in the range 30-39.

You can then have three global variables to keep track of state, and in main you can then split things out, and make decisions based on the latest known state of all 3 imps:

`imp1state <- 0;
imp2state <- 0;
imp3state <- 0;

function Main(value) {
// Translate each imp’s state into a simple number, without the tens prefix
if (value >=10 && value <=19) imp1state = value-10;
if (value >=20 && value <=29) imp1state = value-20;
if (value >=30 && value <=39) imp1state = value-30;

// If imp1 is on and imp2 is on (ie imp1 sent 11 and imp2 sent 21 most recently) then do something
if (imp1state == 11 && imp2state == 21) {

Thanks, Hugo.

I am a bit confused why you are subtracting incrementally from the incoming value variable;

if (value >=10 && value <=19) imp1state = value-10;

So if imp 1 is writing to imp 2 a “10”, by subtracting 10, youll be setting value to 0, correct? why not just write a “0” ?

They have different offsets so that a single receiver can determine which sender sent the message. If you just received a zero, you don’t know which imp sent it to you.

There are better ways of doing this (eg, parsing the URL) but I did this in the hope that it’d be clearer to you.

There is a bug though, the last bit should read:

// If imp1 is on and imp2 is on (ie imp1 sent 11 and imp2 sent 21 most recently) then do something if (imp1state == 1 && imp2state == 1) { //something }

Hey Hugo, I think their is a misunderstanding. I am aware a variable cannot be two numbers simultaneously. Lets backtrack a bit to explain my comprehension of how the agent system works;

so, in my “Main” function, it is reading an incoming value. I have the same functions throughout each imp. I am also assigning each imp the variable name “value”. I figured this wasn’t an issue since variables are native to each imp, and i am not writing a string with the same characters. (which would be obviously an issue if I was sending the string “value” from all imps).

So, I have imp 1 that is sending a “1” to the agent to be received by all the other imps and itself. thats fine. the imps receive the “1”. from imp 2, I am sending a “2” to all of the other imps, and… you get the idea.

What I mean’t by “simultaneously” is that since imp 2 is sending a 2, and imp3 a 3, when I view this in the log of imp1, it reads the variables, but seems to only print the value, and “break” out of the loop, as oppose to what I would think continuously update the character from the coinciding imps. it does not (from viewing in the server log). this is where I am at. I had implemented your suggestion, but it seems as if I am in the same boat as before.

I’m afraid I don’t quite get what you’re doing. Your code was assuming a variable could be two things at once…

Events are discrete - they are not continuous. If an imp sends another imp a value, the receive handler will be called once, not continuously.

My code saves the events from each imp separately, so whenever an event is received, you can see the previous events received from the other imps, allowing you to perform actions based on the state of all the other imps, vs just the last one that sent you a message.

ah, I see how it works now.

I am assuming since this is a switch, we can send a number, say imp2 sends a 2.

we lets say +1 the incoming value then assign it to imp2state.

we test the condition, then reset the imp2state to 0 if else, correct?

The thing is you need to know which imp the message came from. Unless each imp (a) knows which one it is and (b) communicates this to the other imps when it sends a message, the receiver has no way to know.

…which is why I added 10 for imp1, 20 for imp2 and 30 for imp3.