Developer Tool Template works but in Yaml doesn't?

So, i’ve been learning how to extract JSON values, and I can get some of the values from the data below from the code below, but when I look at the states under Dev Tools I can see some of my sensors are ok while others (like kitchen_humidity_local) says ‘unavailable’, but when I try the same template from the yaml code in Developer Tools it works ok, I’m a little confused, any help.

json data…
{
“wh25”: [
{
“intemp”: “19.8”,
“unit”: “C”,
“inhumi”: “74%”,
“abs”: “29.64 inHg”,
“rel”: “29.64 inHg”
}
],
“ch_aisle”: [
{
“channel”: “1”,
“name”: “”,
“battery”: “0”,
“temp”: “20.0”,
“unit”: “C”,
“humidity”: “70%”
},
{
“channel”: “2”,
“name”: “”,
“battery”: “0”,
“temp”: “21.2”,
“unit”: “C”,
“humidity”: “66%”
},
{
“channel”: “3”,
“name”: “”,
“battery”: “0”,
“temp”: “19.6”,
“unit”: “C”,
“humidity”: “78%”
},
{
“channel”: “4”,
“name”: “”,
“battery”: “0”,
“temp”: “19.7”,
“unit”: “C”,
“humidity”: “75%”
}
],
“ch_soil”: [
{
“channel”: “1”,
“name”: “”,
“battery”: “1”,
“humidity”: “–”
}
]
}

code

  - platform: rest
    name: gw1100a-wifif673_local
    json_attributes:
      - wh25
      - ch_aisle
    resource: http://192.168.1.167/get_livedata_info
    scan_interval: 60
    method: GET
    value_template: "OK" 

  - platform: template
    sensors:
      study_temp_local:
        value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'wh25')[0]['intemp'] }}"
        unit_of_measurement: "°C"
      study_humidity_local:
        value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'wh25')[0]['inhumi'] }}"
      bedroom_temp_local:
        value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'ch_aisle')[0]['temp'] }}"
        unit_of_measurement: "°C"
      bedroom_humidity_local:
        value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'ch_aisle')[0]['humidity'] }}"
        unit_of_measurement: "%"
      kitchen_temp_local:
        value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'ch_aisle')[1]['temp'] }}"
        unit_of_measurement: "°C"
      kitchen_humidity_local:
        value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'ch_aisle')[1]['humidity'] }}"
        unit_of_measurement: "%"    

Can you also format the JSON as code so we can copy/paste it without having to fix all the quotes please?

The reason for your error is that the JSON value for the kitchen humidity is "75%" not "75" but you’ve given it a unit_of_measurement so it must be a number. There will be an error in the log about non-numeric values. You want:

value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'ch_aisle')[1]['humidity'][:-1] }}"

which will knock the last character off, or my preferred solution:

value_template: "{{ state_attr('sensor.gw1100a_wifif673_local', 'ch_aisle')[1]['humidity']|select('in','-.0123456789')|join }}"

which will filter out any character not in that '-.0123456789' string.

As an alternative to the RESTful sensor and a bunch of (legacy-format) template sensors, you could use the RESTful integration with this code going directly into configuration.yaml at the top level, not under sensor:

rest:
  - resource: http://192.168.1.167/get_livedata_info
    scan_interval: 60
    sensor:
      - name: Study Temp Local
        value_template: "{{ value_json['wh25')[0]['intemp'] }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: Study Humidity Local
        value_template: "{{ value_json['wh25')[0]['inhumi']|select('in','-.0123456789')|join }}"
        unit_of_measurement: "%"
        device_class: humidity

      - name: Bedroom Temp Local
        value_template: "{{ value_json['ch_aisle')[0]['temp'] }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: Bedroom Humidity Local
        value_template: "{{ value_json['ch_aisle')[0]['humidity']|select('in','-.0123456789')|join }}"
        unit_of_measurement: "%"
        device_class: humidity

      - name: Kitchen Temp Local
        value_template: "{{ value_json['ch_aisle')[1]['temp'] }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: Kitchen Humidity Local
        value_template: "{{ value_json['ch_aisle')[1]['humidity']|select('in','-.0123456789')|join }}"
        unit_of_measurement: "%"
        device_class: humidity

That looks like an Ecowitt hub, have you tried the HA integration ?

Hi, i have, but i have two HA’s running, and the integration can only send to one Custom Server, hence exploring pulling the json data, thx

many thanks, here’s the json

{ "wh25": [{ "intemp": "19.8", "unit": "C", "inhumi": "74%", "abs": "29.64 inHg", "rel": "29.64 inHg" }], "ch_aisle": [{ "channel": "1", "name": "", "battery": "0", "temp": "20.0", "unit": "C", "humidity": "70%" }, { "channel": "2", "name": "", "battery": "0", "temp": "21.2", "unit": "C", "humidity": "66%" }, { "channel": "3", "name": "", "battery": "0", "temp": "19.6", "unit": "C", "humidity": "78%" }, { "channel": "4", "name": "", "battery": "0", "temp": "19.7", "unit": "C", "humidity": "75%" }], "ch_soil": [{ "channel": "1", "name": "", "battery": "1", "humidity": "--" }] }

btw, i’m still only pulling the temp values, and the humidity ones say unavailable, any ideas, here’s the code (i’d tweaked due to an error that yaml check threw-up)

# get data from local ecowitt rest api  
  - resource: http://192.168.1.167/get_livedata_info
    scan_interval: 60
    sensor:
      - name: Study Temp Local
        value_template: "{{ value_json['wh25'][0]['intemp'] }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: Study Humidity Local
        value_template: "{{ value_json['wh25'][0]['inhumi'][:-1] }}"
        unit_of_measurement: "%"
        device_class: humidity

      - name: Bedroom Temp Local
        value_template: "{{ value_json['ch_aisle'][0]['intemp'] }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: Bedroom Humidity Local
        value_template: "{{ value_json['ch_aisle'][0]['inhumi'][:-1] }}"
        unit_of_measurement: "%"
        device_class: humidity

      - name: Kitchen Temp Local
        value_template: "{{ value_json['ch_aisle'][1]['intemp'] }}"
        unit_of_measurement: "°C"
        device_class: temperature

      - name: Kitchen Humidity Local
        value_template: "{{ value_json['ch_aisle'][1]['inhumi'][:-1] }}"
        unit_of_measurement: "%"
        device_class: humidity

If you’re still exploring options and already have a MQTT server running, you could use this:

Then you could point both HA servers to your MQTT server and get the data that way. That may or may not be easier than what you’re trying to do now.

The ch_aisle keys are temp and humidity not intemp and inhumi.

You can fake up value_json:

{% set value_json = { "wh25": [{ "intemp": "19.8", "unit": "C", "inhumi": "74%", "abs": "29.64 inHg", "rel": "29.64 inHg" }], "ch_aisle": [{ "channel": "1", "name": "", "battery": "0", "temp": "20.0", "unit": "C", "humidity": "70%" }, { "channel": "2", "name": "", "battery": "0", "temp": "21.2", "unit": "C", "humidity": "66%" }, { "channel": "3", "name": "", "battery": "0", "temp": "19.6", "unit": "C", "humidity": "78%" }, { "channel": "4", "name": "", "battery": "0", "temp": "19.7", "unit": "C", "humidity": "75%" }], "ch_soil": [{ "channel": "1", "name": "", "battery": "1", "humidity": "--" }] } %}

and paste the whole YAML into the template editor to see if the values are resolved correctly. With temp and humidity:

A note for anyone reading this, as it seems to come up frequently: the template editor DOES NOT implement the sensors. It’s merely turning the Jinja templates in the value_template lines into text output so you can see if the YAML will work when you put it into the config.

dope!, thank you, silly mistake on my part. Thank you for your help on this pkscout and Troon

1 Like