Generic water level sensor input

Hi, i’m Nymea user since few months ago, i found it marvelous, thank you for your amazing job with it.

I’m already playing with the generic water level sensor, for that i’m using a hr-sr04 connected to a tasmota device to meassure the distance between the sensor and the water surface. That is working well, but wen i’m setting up Nymea things i ran into problems.

I have created the next things:

  • Tasmota device.
  • Internal mqtt client.
  • Generic water level sensor.

After setting up the mqtt client, i created the following magic script (based on a working temp sensor):

import QtQuick 2.0
import nymea 1.0

Item {
        ThingEvent {
            thingId: "{060e00dd-b96e-4b05-baf6-e30b5b2c7ac7}" // Tank sensor
            eventName: "triggered"
            onTriggered: {
                console.log("received from tank sensor:", JSON.stringify(params));
                var data = JSON.parse(params["data"]);
               tank1.value = data.SR04.Distance;
               console.log("value: ",tank1.value," dist: ",data.SR04.Distance);

        ThingState {
            id: tank1
            thingId: "{e761c17f-4e8a-474e-bc13-0d9f12dec91b}" // Generic water level sensor
            stateName: "waterLevel"


Console log example output:
9: received from tank sensor: {"27ec8baf-0c13-4d0a-aaee-313582592695":"24c87bc3bcdd4d46b05d4b96a39ab581/sonoff/SENSOR","8af98566-79d9-4e65-b1dc-9067e4f93af1":"{\"Time\":\"2022-01-16T21:58:01\",\"SR04\":{\"Distance\":37.895}}","data":"{\"Time\":\"2022-01-16T21:58:01\",\"SR04\":{\"Distance\":37.895}}","topic":"24c87bc3bcdd4d46b05d4b96a39ab581/sonoff/SENSOR"}

14: value: 0 dist: 37.895

My problem is that tank1.value is always zero. I tried to put in it different values, between 0 and 1, between 0 and 100… but It always shows zero (and the sensor visualization too). I tried to change other sensors value from the same script and it works fine. But my new generic water level sensor refuses to work.

Can you help me figure out what I’m missing? Thank you in advance.

I think you need to set the “input” state, not the “waterLevel” state.

        ThingState {
            id: tank1
            thingId: "{e761c17f-4e8a-474e-bc13-0d9f12dec91b}" // Generic water level sensor
            stateName: "input"

This will then be mapped to the waterLevel state internally in the generic water level thing according to the thing settings min/max range.

Thanks Michael, unfortunately that was an error on my last tests, but using stateName as input i got the same problem.

It is strange, because if i put a hardcoded value, like tank1.value = 50; at the console log tank1.value is zero anyway.

If you open the generic water level sensor thing, and go to the details view, you can drag the input value slider and should see the output value changing. Is that working for you? Moving that slider would basically be the same as writing something to it with the script.

Thanks Michael! finally with your tips i made it work.

The input value slider only allowed me to put values between 0 and 1, although i had configured minimun and max values between 0 and 1000. As you said, changing it, the graph were displaying the output value acording to the tank capacity config.

The “value zero” problem persist, its was desorientating, but if you put a value between 0 and 1 the sensor receives the value an it is represented on the sensor graph.

There were three errors on my configs:

  • min and max values are not considered when you set the value from the script.
  • i was sending values greater than 1 for the input.
  • when i did some test with values between 0 and 1 i did not check the output value, console log only and there were always at 0 the input value.

Thank you again, Regards

Unless there is a bug in the code, the min/max values should be considered… Setting a max value of e.g. 77, would cause the output value to go to 77 when the input is set to 1. If that’s not working, I would say that’s a bug.