Restful Sensor - First Try

Hi all !

I’m trying to get a simple value in a simple JSON :slight_smile: 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?

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.

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 … :frowning_face:

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 :slight_smile:

Any idea on how to read this bad json though ?

Thanks for your answers …

OK, thank you, I learned something :slightly_smiling_face:

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 …

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 :slight_smile: Bye !

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 :wink: