Currently, I am working on having serial communication between an RPI4 and Arduino Mega.
If I send an un-nested JSON message to the Pi I can see it and read it and assign it to a sensor with no issue. but as I have multiple devices I’ve decided to send nested JSON messages.
my current output is:
b1 = unavalible
Any ideas as to why the sensor data is not displaying? if I receive it unnested it is fine, just having troubles working out the correct formating for nested serial
my template looks like:
Imitate available variables:
{% set value_json = {"nest1":{"b1":36,"b2":51.51},"nest2":{"s1":-0.13,"s2":20}
}%}
'{{ value_json['nest1']['b1'] }}'
'{{ value_json['nest1']['b2'] }}'
'{{ value_json['nest2']['s1'] }}'
'{{ value_json['nest2']['s2'] }}'
which gives out:
'36'
'51.51'
'-0.13'
'20'
when I look at my Developer Tools states I can see the sensor as sensor.serial_sensor which is reading state:
{"nest1":{"b1":36,"b2":51.51},"nest2":{"s1":-0.13,"s2":20}
Displaying nicely in the attributed collum as:
nest1:
b1: 36
b2: 51.51
nest2:
s1: -0.13
s2: 20
Which is great because its there!
With this I’m trying to use the sensor template as follows:
sensor:
- platform: serial
serial_port: /dev/ttyACM0
baudrate: 9600
- platform: template
sensors:
b1:
friendly_name: "b1"
unit_of_measurement: "°C"
value_template: "{{ state_attr('sensor.serial_sensor', value_json['nest1']['b1']) }}"
b2:
friendly_name: "b2"
unit_of_measurement: "pH"
value_template: "{{ state_attr('sensor.serial_sensor', value_json['nest1']['b2']) }}"
s2:
friendly_name: "s2"
unit_of_measurement: "mg/L"
value_template: "{{ state_attr('sensor.serial_sensor', value_json['nest2']['s2']) }}"
as per:
Cheers