I’ve just got a Geiger counter going and sending data to HA over MQTT in JSON format from an 8266 with my own sketch. Works fine.
My intention then was to power it up from a USB port on my NUC running HA and I had a thought - wouldn’t it be better to send the data via the USB port and save using WiFi. That way I could run the thing using a, Arduino nano that I have lying around.
I’ve read the guides and stuff, but I just can’t work out how to use a template with the incoming message (ie the JSON string). I have however managed to read the different values if I format the incoming string/message as CSV.
I’d prefer to do it the JSON way though so any help would be appreciated.
The incoming string is received OK thus
sensor:
- platform: serial
name: nano
serial_port: /dev/ttyUSB1
baudrate: 115200
For anyone else looking for this, I couldn’t find a way!
Reading various posts having searched around the problem, I get the impression that the string that gets into the ‘state’ of the sensor.nano (in my case) entity is always a String Object and never a JSON Object, and there is no way round it.
I did manged to do it using Node-Red using a Serial input node followed by a JSON parsing node then a 4 output Function node with code…
var msgT = {};
var msgH = {};
var msgC = {};
var msgU = {};
msgT.payload = msg.payload.Room.Temperature;
msgH.payload = msg.payload.Room.Humidity;
msgC.payload = msg.payload.Geiger.CPM;
msgU.payload = msg.payload.Geiger.uSvph;
return [msgT, msgH, msgC, msgU];
This feeding four Entity nodes.
However, in the end I resorted to the CSV method with the template sensors decoding the nano sensor as configured in my first post.
I came across tojson (no undersore!) somewhere and tried that with the filter (but no extra brackets) but did not see the from_json anywhere.
I’ll give it a try now with a spare 8266 just to see if you are right and report back.
Not inclined to change from the CSV format now though.
Thanks