USB/Serial input in JSON format

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

The JSON string looks like

{"Geiger":{"CPM": 15, "uSvph": 0.14},"Room":{"Temperature": 20.0, "Humidity": 60}}

and when I use the Template editor with

{{states("sensor.nano")}}

it shows me the correct JSON string. I just cannot figure out what to do next.

The CSV string is

15, .14, 20.0, 60

Which I can decode individual values using

template:
  sensor:
    - name: humidity
      state: "{{ states('sensor.nano').split(',')[3] | float(default=0) }}"

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.

template:
  sensor:
    - name: counts_per_minute
      state: "{{ states('sensor.nano').split(',')[0] | float(default=0) }}"
    - name: uSvph
      state: "{{ states('sensor.nano').split(',')[1] | float(default=0) }}"
    - name: temperature
      state: "{{ states('sensor.nano').split(',')[2] | float(default=0) }}"
    - name: humidity
      state: "{{ states('sensor.nano').split(',')[3] | float(default=0) }}"

And the finished product (although I do need to do something about that dangling nano!

if the state of sensor.nano was valid json, i.e.

then this would have worked.

{{ (states('sensor.nano') | from_json).Room.Temperature }}

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

to_json converts a dictionary to json. You want to change json into a dictionary, which is from_json. See the docs:

petro

I just tried your suggestion and it works. :smiley:

I don’t think I would have worked that out even if I had read the JSON bit of the Template documentation. I find all that a bit cryptic.

Thanks