Pico1965
(Pico1965)
September 11, 2024, 1:32pm
1
Hiii.
This is an example json file:
{
"database": {
"house": {
"square": 100,
"hight": 5,
"doors": 2,
"windows": 5
},
"garage": {
"square": 20,
"hight": 2.5,
"doors": 1,
"windows": 1
},
"office": {
"square": 150,
"hight": 3,
"doors": 2,
"windows": 5
}
}
}
saved as measurement.json in www folder of home assistant.
I’ve input_select.choice:
My goal is to create 4 sensors:
sensor.square
sensor.hight
sensor.doors
sensor.windows
that take the result from the json file based on the property chosen with the input_select.
If possible, I would prefer to keep a database somewhere in my system because:
more automations could access the data;
the database is complex and subject to frequent changes that cannot be done at sensor level.
Thanks
Possibly a template sensor, I would likke to knmow how to do this too
koying
(Chris B)
September 12, 2024, 9:46am
3
Possible solution:
Load the db into an attribute through a command_line
sensor (states have a 255 char limit, which attributes haven’t):
command_line:
- sensor:
name: json_db
command: cat /config/database.json
value_template: "{{ 'OK' }}"
json_attributes:
- database
Have template sensors to extract the proper value based upon the input_select
:
template:
- sensor:
- name: "JSON db test square"
state: "{{ state_attr('sensor.json_db', 'database')[states('input_select.json_select')]['square']}}"
Pico1965
(Pico1965)
September 12, 2024, 3:21pm
4
Thanks for your attention:
command_line:
- sensor:
name: json_db
command: cat /config/**measurement**.json
value_template: "{{ 'OK' }}"
json_attributes:
- database
This crete the sensor json_db with state “OK” and with attrib the full content of measurement.json file
template:
- sensor:
- name: "JSON db test square"
state: "{{ state_attr('sensor.json_db', 'database')[states('**input_select.choice**')]['square']}}"
Create the sensor: JSON db test square with state:unavailable.
P.S. ** correct code based on my initial request
koying
(Chris B)
September 12, 2024, 3:47pm
5
If you’re running HAOS, it might be the file is at “/homeassistant”, I don’t remember what’s the root there
Pico1965
(Pico1965)
September 12, 2024, 3:50pm
6
The command_line working fine.
Is the data extraction to json failed…
koying
(Chris B)
September 12, 2024, 3:53pm
7
You lack a quote after “states(”, but I guess it’s a typo in the post?
Pico1965
(Pico1965)
September 12, 2024, 3:59pm
8
Correct the post… deleted when I added the asterisks!
koying
(Chris B)
September 12, 2024, 4:52pm
9
Yeah. I tested it so definitely works for me.
Try it in the template editor and check what it says…