Dual DHT22 Sensors, one MQTT Topic - Extract data

I have dual DHT22 sensors connected to a D1 Mini, one gathering temperature & humidity within my greenhouse and the other measuring temperature & humidity outside the greenhouse. The D1 mini is running Tasmota and the data for both sensors is published as a single topic as follows:

{“Time”:“2020-03-12T23:28:24”,“AM2301-04”:{“Temperature”:3.5,“Humidity”:90.8},“AM2301-05”:{“Temperature”:4.4,“Humidity”:86.1},“TempUnit”:“C”}

I’ve set up the following sensors to gather the data in my sensors.yaml but all I get is “unknown” back:

# Greenhouse Temp
- platform: mqtt
  name: "Greenhouse Temperature"
  state_topic: "tele/tasmota/SENSOR"
  device_class: temperature
  unit_of_measurement: "°C"
  value_template: '{{ value_json.AM2301-04.Temperature }}'
- platform: mqtt
  name: "Greenhouse Humidity"
  state_topic: "tele/tasmota/SENSOR"
  device_class: humidity
  unit_of_measurement: "°C"
  value_template: '{{ value_json.AM2301-04.Humidity }}'

When I had just one sensor it worked perfectly but the data was posted as follows:

{“Time”:“2020-03-12T23:38:48”,“AM2301”:{“Temperature”:3.5,“Humidity”:90.6},“TempUnit”:“C”}
and was retrieved with

value_template: '{{ value_json.AM2301.Temperature }}'

i.e no -04 or -05 after the AM2301 so I’m guessing that’s the cause of problem?

I’m sure its something simple but I’ve spent hours trying to solve this using templating on the developer tools page to no avail so any suggestions would be greatfully received.
Thanks
Tony

Why not use separate topics?

Did u see one is AM2301-04 and the other is AM2301-05 so think u need to change the Jason reading

value_json.AM2301-04.Temperature

value_json.AM2301-05.Temperature

I agree that would be the most logical way to do it but can’t see how to setup separate topics on the Tasmota configuration page. When I enable the second sensor it automatically appends the data to that from the first and adds the GPIO pin number to the sensor name i.e AM2301-04 & AM2301-05.
Regards
Tony

Yes I did but I only included the sensor configuration for one of the sensors in my post as an example. I actually have four sensors configured in total Temp & Humidity for AM2301-04 (the two I posted) and Temp & Humidity for AM2301-05 - all four report “unknown”.

Winging this and old thought just pop into head

value_json.[“AM2301-05”].Temperature

Can’t remember if it double quote or single quote
After the [
Give that a try

Thanks @myle but unfortunately neither options work, the double quotes give the following error in the log:

Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data ['value_template']. Got '{{ value_json.["AM2301-04"].Temperature }}'. (See /home/homeassistant/.homeassistant/sensors.yaml, line 80). Please check the docs at ......

Using single quotes gives the following in the log and results in HA not starting.

2020-03-12 23:30:18 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while parsing a block mapping
  in "/home/homeassistant/.homeassistant/sensors.yaml", line 81, column 3
expected <block end>, but found '<scalar>'
  in "/home/homeassistant/.homeassistant/sensors.yaml", line 86, column 36
2020-03-12 23:30:18 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
  in "/home/homeassistant/.homeassistant/sensors.yaml", line 81, column 3
expected <block end>, but found '<scalar>'
  in "/home/homeassistant/.homeassistant/sensors.yaml", line 86, column 36

found this I wrote a long time a go

Did you try using JSONPath, looking it up like an array?

So maybe:

 value_template: '{{ value_json.AM2301-04[0].value }}'

Try this format:

# Greenhouse Temp
- platform: mqtt
  name: "Greenhouse Temperature"
  state_topic: "tele/tasmota/SENSOR"
  device_class: temperature
  unit_of_measurement: "°C"
  value_template: "{{ value_json['AM2301-04'].Temperature }}"
- platform: mqtt
  name: "Greenhouse Humidity"
  state_topic: "tele/tasmota/SENSOR"
  device_class: humidity
  unit_of_measurement: "°C"
 value_template: "{{ value_json['AM2301-04'].Temperature }}"

Thanks @NK553 but that doesn’t work either.

@Akriss - Yes I’ve tried that as suggested by @myle above.

Argghhh go to esphome.

Hay bro Been Thinking more about you problem
after setting up the same mqtt messages

the sensors

  - platform: mqtt
    name: "Greenhouse Temperature 04"
    state_topic: "tele/tasmota/SENSOR"
    device_class: temperature
    unit_of_measurement: "°C"
    value_template: "{{ value_json['AM2301-04'].Temperature }}"
  - platform: mqtt
    name: "Greenhouse Humidity 04"
    state_topic: "tele/tasmota/SENSOR"
    device_class: humidity
    unit_of_measurement: "°C"
    value_template: "{{ value_json['AM2301-04'].Humidity }}"

  - platform: mqtt
    name: "Greenhouse Temperature 05"
    state_topic: "tele/tasmota/SENSOR"
    device_class: temperature
    unit_of_measurement: "°C"
    value_template: "{{ value_json['AM2301-05'].Temperature }}"
  - platform: mqtt
    name: "Greenhouse Humidity 05"
    state_topic: "tele/tasmota/SENSOR"
    device_class: humidity
    unit_of_measurement: "°C"
    value_template: "{{ value_json['AM2301-05'].Humidity }}"    

and i the sensor see

so it work for me

Question are you on a MAC computer

are you using the right Quote marks

I had to change them when I copy pasted

{“Time”:“2020-03-12T23:28:24”,“AM2301-04”:{“Temperature”:3.5,“Humidity”:90.8},“AM2301-05”:{“Temperature”:4.4,“Humidity”:86.1},“TempUnit”:“C”}

and yours

{“Time”:“2020-03-12T23:28:24”,“AM2301-04”:{“Temperature”:3.5,“Humidity”:90.8},“AM2301-05”:{“Temperature”:4.4,“Humidity”:86.1},“TempUnit”:“C”}

Thanks for your efforts @myle , I’m at work now but will try again when I get back home. The only difference I can see between what I tried earlier and what you’ve tried is that I’m pretty sure I had a . between value_json and the first [ so I tried

"{{ value_json.['AM2301-04'].Temperature }}"

rather than

"{{ value_json['AM2301-04'].Temperature }}"

as suggested by yourself. That may well be the difference, I’ll let you know how I get on.

good spotting on the dot I just type it out let my fingers do the work.

Best of luck

Thanks @myle There must have been something screwed up in my syntax somewhere because even when I modded my sensor.yaml along the lines of yours it still didn’t work! In the end I copied and pasted your syntax from the post above into my yaml and that solved it.
Thanks again.
Tony

good to see you got it sorted.

by pasted my code in telling me that could be the double quote not all double are the same