Does value template support dashes in sensors?

I’m using TASMOTA firmware with my Sonoff’s and in a recent update they have added the ability to have multiple DHT sensors and thus append -GPIO to each sensor. This seems to be unsupported by Home Assistant value templates. i.e.:

[homeassistant.helpers.template] Error parsing value: 'dict object' has no attribute 'AM2301' (value: {"Time":"2017-05-07T09:08:27", "AM2301-14":{"Temperature":20.8, "Humidity":60.4}, "TempUnit":"C"}, template: {{ value_json.AM2301-14.Temperature }})

This sensor before the -14 worked fine: - platform: mqtt name:

"Master Bedroom Temp" state_topic: "tele/master_bedroom_th/SENSOR" unit_of_measurement: "°C" qos: 1 value_template: '{{ value_json.AM2301-14.Temperature }}'

The format of the JSON data is:

{"Time":"2017-05-07T09:33:27", "AM2301-14":{"Temperature":21.1, "Humidity":61.2}, "TempUnit":"C"}

Is there any way to either escape the - or quote the json_value in such a way that it doesn’t balk at the dash?

Try: value_json[“AM2301-14”].Temperature

I’m afraid that didn’t work:

2017-05-07 10:10:50 ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['value_template']. Got '{{ value_json.["AM2301-14"].Temperature }}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.mqtt/

Sorry, I don’t know much about MQTT sensors, but the template JSON parse is solid.
You can paste this into the template parser in developer tools and extract your value…

{%set value_json={"Time":"2017-05-07T09:33:27", "AM2301-14":{"Temperature":21.1, "Humidity":61.2}, "TempUnit":"C"}%}
{{ value_json["AM2301-14"].Temperature }}

Im my repeated experience the template parser has about as much in common to actual value templates as fiction has to fact.

You can test that HA doesn’t accept the template by creating it in your system and testing the config (not even any need to restart.

FYI, This is the solution… Awareness of double and single quotes is important.

  - platform: mqtt
    name: "Master Bedroom Temp"
    state_topic: "tele/master_bedroom_th/SENSOR"
    unit_of_measurement: "°C"
    qos: 1
    value_template: "{{ value_json['AM2301-14'].Temperature }}"

Thanks to https://github.com/home-assistant/home-assistant/issues/3830

1 Like

I tried to follow your solution, but this did not work for me, but the following did tho.

  • platform: mqtt
    name: “Landing Temperature”
    state_topic: “tele/HW/SENSOR”
    value_template: “{{ value_json.AM2301.Temperature }}”
    unit_of_measurement: “°C”

thought i would share :slight_smile:

Yes, this would be because Tasmota reverted to not requiring the -GPIO if you only had one sensor (as it was confusing too many people).

If you were to connect multiple AM2301 sensors I think this format would still be required.