Hi all !
I’m trying to get a simple value in a simple JSON with the RestFul Sensor …
but result is always “unknown” …
What am I doing wrong ?
My JSON :
{
"option_tarifaire" : 1,
"tarif_courant" : 2,
"isousc" : 60,
"conso_base" : 0,
"conso_hc" : 000852348,
"conso_hp" : 001683809,
"conso_hc_b" : 0,
"conso_hp_b" : 0,
"conso_hc_w" : 0,
"conso_hp_w" : 0,
"conso_hc_r" : 0,
"conso_hp_r" : 0
}
My configuration.yaml :
sensor:
- platform: rest
resource: http://192.168.1.42/data.json
name: conso_hc
value_template: '{{ value_json.conso_hc }}'
The URL is OK with curl in Putty …
The RestFul Sensor example with external IP works fine …
Does the URL *.json the problem ?
Thanks for your help,
Are you getting any warnings or errors in the log? How do you have logger
configured? What do you get if you remove value_template
from the configuration of the REST sensor?
tjntomas
(Tomas Jansson)
January 6, 2020, 8:43pm
3
Tintolo:
“conso_hc” : 000852348,
The json is invalid, test it here: https://jsonformatter.curiousconcept.com/
It seems that leading zeroes in a number, 000852348
, is not valid json. Does it work if you instead try to access one of the other values, for instance “isousc”? If so, the reason is the invalid json and you would need a bit of code to access the value.
l_o_k_i
(Jim)
January 6, 2020, 9:22pm
4
It doesn’t seem to be a problem when testing in the template editor (Developer Tools, Template tab). I used this:
{% set my_test_json = {
"option_tarifaire" : 1,
"tarif_courant" : 2,
"isousc" : 60,
"conso_base" : 0,
"conso_hc" : 000852348,
"conso_hp" : 001683809,
"conso_hc_b" : 0,
"conso_hp_b" : 0,
"conso_hc_w" : 0,
"conso_hp_w" : 0,
"conso_hc_r" : 0,
"conso_hp_r" : 0
}
%}
{{ my_test_json.conso_hc }}
and it returned: 852348
Please check again the resource URL
You’re not actually creating a JSON formatted string. You’re creating a Python dict
. The problem is with the JSON parsing, so this isn’t a valid test.
This is a more appropriate test:
{% set value = '{"conso_hc" : 000852348}' %}
{{ value|from_json }}
You’ll see this results in an error. However, if you change 000852348
to 852348
it works ok.
Tested in the template editor before asking, and yes it works in it … but not in the sensor …
Test in https://jsonformatter.curiousconcept.com/ is KO … invalid answer are on the two “numbers” conso_hc and conso_hp
Validator Output
Error:Invalid number.[Code 141, Structure 19]
Error:Invalid number.[Code 141, Structure 23]
Formatted JSON Data
{
"option_tarifaire":1,
"tarif_courant":2,
"isousc":60,
"conso_base":0,
"conso_hc":000852348,
"conso_hp":001683809,
"conso_hc_b":0,
"conso_hp_b":0,
OK, problem is my json … but i cannot change it …
I will work on “logger”, my HA installation is really new, i don’t have everything installed yet
Any idea on how to read this bad json though ?
Thanks for your answers …
l_o_k_i
(Jim)
January 6, 2020, 10:23pm
7
OK, thank you, I learned something
1 Like
Is the value decimal, or could it possibly be hex or octal? Kind of hard to be sure given the formatting of the number.
Assuming it’s decimal, you could try this:
value_template: "{{ value|regex_findall_index('\"conso_hc\"\s*:\s*([0-9]*)')|int }}"
Always an integer, decimal, no letters, i’ll try your RegEx,
Thanks …
Tintolo
January 6, 2020, 10:51pm
10
unknown escape sequence at line 73, column 65:
... gex_findall_index('\"conso_hc\"\s*:\s*([0-9]*)')|int }}"
The simple quote …
I’ll try another RegEx tomorrow, it’s late here Bye !
l_o_k_i
(Jim)
January 6, 2020, 10:59pm
11
try this:
value_template: '{{ value|regex_findall_index("\"conso_hc\"\s*:\s*([0-9]*)")|int }}'
Sorry, my bad. (I forgot to escape the escape characters.) Try this:
value_template: "{{ value|regex_findall_index('\"conso_hc\"\\s*:\\s*([0-9]*)')|int }}"
Or it looks like @l_o_k_i ’s suggestion should work, too.
It works, tried @l_o_k_i 's solution,
I’ll try to have a graph now, and totalise for a day,
Thanks