Help templating with JSON

Hey guys!

So I’ve tried to understand templating, but it really got me. :frowning:

I have a text file with the following json values:

 [{"name":"Bathroom","id":"1.7","indoor_temp":24.4,"humidity":44.9,"outdoor_temp":22,"time":"2019-10-17 16:00"},{"name":"Bedroom","id":"1.2","indoor_temp":24.6,"humidity":47.6,"outdoor_temp":22,"time":"2019-10-17 16:00"},{"name":"Livingroom","id":"1.1","indoor_temp":25.3,"humidity":34.2,"outdoor_temp":22,"time":"2019-10-17 16:00"},{"name":"Wardrobe","id":"1.3","indoor_temp":25.6,"humidity":45.3,"outdoor_temp":22,"time":"2019-10-17 16:00"}]

Could you guys please help me, how could I make 4 sensors out of it?
Or do I need to have it in 4 different files?

Thanks a lot!

One file will do. As long as the values are on the last line of the file you can use this:

to create sensors. However as it does not support attribute templates you will have to create 8 sensors, one of temperature and one of humidity for each room.

I am aware of that, I need to use File sensor for it, but I have no idea how to separate the informations, having in this text file.
it looks like this.

[{“name”:“Bathroom”,“id”:“1.7”,“indoor_temp”:24.4,“humidity”:44.9,“outdoor_temp”:22,“time”:“2019-10-17 16:00”},{“name”:“Bedroom”,“id”:“1.2”,“indoor_temp”:24.6,“humidity”:47.6,“outdoor_temp”:22,“time”:“2019-10-17 16:00”},{“name”:“Livingroom”,“id”:“1.1”,“indoor_temp”:25.3,“humidity”:34.2,“outdoor_temp”:22,“time”:“2019-10-17 16:00”},{“name”:“Wardrobe”,“id”:“1.3”,“indoor_temp”:25.6,“humidity”:45.3,“outdoor_temp”:22,“time”:“2019-10-17 16:00”}]

for some reason it was merged into one line up there.

for first file sensor…

{{ value_json[0].name }}

for second…

{{ value_json[1].name }}

You’ll need 20 sensors for all info, increment the number and attribute to get desired result.

That’s just word-wrapping in your text editor. There are no newline characters, so it shows as one line.

Look at this and note the numbers in the red circles.

There may be a problem with this being interpreted as a string rather than JSON data so try this first and see if it works

sensor:
  - platform: file
    file_path: /home/user/.homeassistant/sensor-data.txt  # change this to the path to your file
    name: Bathroom Temperature
    value_template: "{{ value_json[0].indoor_temp }}"

If it does work then this is the template for Bathroom Humidity:

    value_template: "{{ value_json[0].humidity }}"

Bedroom Temp:

    value_template: "{{ value_json[1].indoor_temp }}"

Livingroom Temp:

    value_template: "{{ value_json[2].indoor_temp }}"

Get the idea?

I’m guessing there are only 8 values there (indoor_temp and humidity for each room) that are required. Maybe a ninth for outdoor_temp (they all seem to be the same).

Yeah I wasn’t looking at the data, just counting the key value pairs in the dictionaries.

I’ve been brushing up on my regex expressions if we have this problem again.

Lol, I’m trying to avoid it. It would be great if all of these integrations followed the same ideals when creating sensors. They should all emulate MQTT imo. Then these goofy issues would be solved. 1 file sensor, x number of template sensors. Or better yet, seeing that it’s a list of 4 senors, create 4 sensors with attributes. There’s so many options.

1 Like

thanks guys, you’re all awesome! :slight_smile:

@tom_l’s solution helped me, I have the raw datas.

1 Like

What is creating the text file? Do you have any control over this source and how it reports its data?

One more question.
What should trigger the update?
I’ve updated the text file several times now, and HA still showing me the data from 15:23, even though it has data source from 17:10. It’s like the file has been cached…

*I’ve even restarted HA, and datas are not updated. :confused:

okay that’s solved too.
for some reason in the root directory of .homeassistant it didn’t want to refresh.
moved to another directory, whitlested that one, and now it’s fine.