How to reverse a value?

I have a soil moisture sensor that gives me values from 2850 (dry) to 1250 (in water). I would like to convert these into percentage values, i.e. 0-100%.
My problem is that I can only convert from small to large with the range node, i.e. the dry value is displayed as 100%.
Can I also reverse this? If so, how?

Bildschirmfoto 2023-11-22 um 12.33.13

In a simple change or function node AFTER the range node, deduct the payload from 100

1 Like

Subtract the result from 100?

Snap!

1 Like

I’m sorry but how exactly do I express that in JS/a function node?

I expect this might work
msg.payload = 100 - msg.payload
Did you try that?

1 Like

You can reverse the target values in range node.

4 Likes

That works! Awesome, thank you! :slightly_smiling_face:

Hmm... Is it linear? Quite often, there is a nonlinear characteristic line ...

I’m not sure what you/that means.
do you like to explain?

Is half way between 1250 and 2850 (2050) equivalent to 50%? If yes, then that means the it is a linear relationship, which would be represented on a graph by a straight line. If not, which means the line is curved, then it is a non-linear relationship.

I think it’s pretty much half now. I still don’t understand the point. Does that mean I have to change something?


No you don't need to change anything.
It will be fine as long as you work out what reading is too wet or too dry for your Monstera's comfort and water it so it stays within the limits.
I think overwatering is worse than underwatering.

Note that some soil moisture sensors corrode and quickly fail when left in contact with wet soil!

1 Like

You misunderstand (I think). The range node will certainly give you 50% for an input of 2050, the question is does 2050 coming out of the sensor represent 50% humidity. In your case, as @jbudd suggests, it probably doesn't matter as all you need is to know what % the gauge shows when it is what you consider to the correct humidity for the plant.

By the way, using the Range node with the values reversed is more efficient than adding a Function node.

2 Likes

Well that’s raw values. I put the sensor first dry on my table (2800), afterwards I put it in a glass of water (1300), now I’m experimenting and watch when I’d usually would water my plant and what’s the value of it. I’ll change the representative colours of the gauge for it then.
I think every sensor and soil is a bit different so I have to play with it a bit.

I know it’s the wrong forum/topic here. But I put still my raw values in influx and over to grafana. Could someone give me a hint how to edit the value in grafana to get also there the percentage representation?

I can, but it is late and I will need to do some maths. I will post something tomorrow if no-one else does.

1 Like

The formula you need to apply in Grafana is, I believe

percent = 100*(2850 - value) / (2850-1250)

Which is the same as

percent = 0.0625 * (2850-value)

If you look at the query in Grafana, at the right hand side is a pen symbol. Click that and it shows the query it is going to run. You should see something like
SELECT mean("value") FROM "your_measurement" WHERE ....
You should be able to edit that to something like
SELECT (2850-mean("value"))*0.0625 FROM "your_measurement" WHERE ....
"value" and "your_measurement" will be whatever your field and measurement names are of course.

Once you have done this you can't easily go back to the original query by clicking the pen again, if you do it will mess up the query (but only if you save it of course).
Initially you might like to do it by copying the existing query and editing the new copy, just in case it doesn't work.

1 Like

Thank you so much! Very kind and helpful!

The only thing, I guess I don’t get a basic concept, where do I put the code in grafana? I don’t find any option for it.
Where the query code from influx goes? :thinking:

How are you configuring grafana to show the value at the moment? I expected you to have configured something that looks vaguely like this

If you click the pen in the top right changes to

Which is where you can change the query details.

hmmmm... nope, mine look like this:


and I cant find a pen. Obviously we have somehow a different configuration.
Isn't there maybe another way ?

Oh you are using flux. I see that is being deprecated. I have not moved to influxdb 2 yet. I will see if I can work out how to do the maths in flux. Or you could look at the flux docs for how to do maths on a value.

1 Like