Convert status "on"/"off" to 0/1

Hi,
I am want to us the switch state of a shelly relay in a grafana time series. The status is coming in as "on" or "off". I would like to convert to a number (int/float whatever) so that Grafana can process it, but I get an error in Node-Red. I have the following setup:

And tried this in function:

if (msg.payload = "off") {
    msg.payload = 0
};
if (msg.payload = "on") {
    msg.payload = 1
};
return msg;

-> I received following output in Node-Red including an error messages:

29.8.2022, 18:11:46[node: debug 5]relay 0 : msg.payload : string[2]
"on"

29.8.2022, 18:11:46[node: debug 4]relay 0 : msg.payload : number
1

29.8.2022, 18:11:46[node: Shelly 1 Europiccola]
msg : error
"Error: A 400 Bad Request error occurred: {"error":"partial write: field type conflict: input field "value" on measurement "europiccola_ext_relay" is type float, already exists as type string dropped=1"}↵"

Any idea?

You are performing an assignment on line-1 and line-4, try changing it to a test for equality like this...

if (msg.payload == "off") {
    msg.payload = 0
}
if (msg.payload == "on") {
    msg.payload = 1
}
return msg;

The semicolons aren't needed on line-3 and line-6

If that's all you want to do, you could shorten the code to this...

if (msg.payload == "off") msg.payload = 0

if (msg.payload == "on") msg.payload = 1

return msg;

If you wanted to only react to incoming "on" or "off" (and ignore everything else), you could do this...

if (msg.payload == "off") {
    msg.payload = 0;
    return msg;
}
else if (msg.payload == "on") {
    msg.payload = 1;
    return msg;
}
return null;
2 Likes

Your database already has some entries which are string eg on / off, so you cannot now save numbers to that.

You could drop the measurement and then recreate.

Hallo @lessi, Is this a La Pavoni Europiccola / Node-red project?

My vintage europiccola has it's own Node-red dashboard too.

How can I do that?

Sorry, my code was pretty bad. I rushed it. But anyway it does not fix the node-red error

Yes, it is. I implemented a two-point temperature controller with a shelly (Boiler temperature and relay switching). Doesn't work too good though. What are you measuring?

I tried dropping it, but it keeps showing up:

root@18d5af1764d:/app# influx
Connected to http://localhost:8086 version 1.8.10
InfluxDB shell version: 1.8.10
> show databases
name: databases
name
----
raspberry
_internal
shelly_database
> use shelly_database
Using database shelly_database
> show measurements
name: measurements
name
----
europiccola_ext_relay
europiccola_ext_temperatur_0
europiccola_ext_temperatur_boiler
europiccola_ext_temperatur_bruehgruppe
europiccola_ext_temperatur_frei
europiccola_ext_temperatur_relay
phase_0_power
phase_1_power
phase_2_power
> drop measurement europiccola_ext_relay
> drop measurement europiccola_temperature_0
> show measurements
name: measurements
name
----
europiccola_ext_relay
europiccola_ext_temperatur_0
europiccola_ext_temperatur_boiler
europiccola_ext_temperatur_bruehgruppe
europiccola_ext_temperatur_frei
europiccola_ext_temperatur_relay
phase_0_power
phase_1_power
phase_2_power

Presumably something is adding data to them as soon as you drop them.

What do you see if you
select * from europiccola_temperature_0

It may help if you spell it correctly. Missing ext and e on the end

That was the reason! Everything works now! Thx

My La Pavoni is plugged into a smart plug with power monitoring.
I can turn it on from the Node-red dashboard and it turns off again after 30 minutes.
The power consumption data (from switch-on to the pressurestat cutting off the power) is used to estimate the current boiler fill level; it refuses to turn on if the estimate is too low.

I did experiment with an IR temperature sensor to plot the boiler temperature too but it wasn't reliable because of the shiny chrome.

I am currently tweeking in Node-red so that in cold weather it uses the last of the cheaper overnight electricity to warm the boiler.

I didn't know that a dashboard can be create in Node-red. I am currently using grafana to display all data. Even though I am not sure whether I could implement a button there.
I am not (yet) interested in the power consumption of the Europiccola. Currently I am more focused on the correct temperature for the perfect capuccino. I am using a shelly 1 and the temperature add on. Unfortunately the sensors are little too big and as I do not want to drill into the machine, the measurements can only be a rough guide.

I didn't know that a dashboard can be create in Node-red.

You can install the node-red-dashboard node that will open up a whole world of possibilities (and sometimes complexities)