Hello, if you can help me with this:
I want to make an automation that does the following.
-every hour it will be executed (trigger), this is not a problem:
trigger:
platform: time_pattern
hours: /1
My problem is in the action, I don’t know how to do it. I have to read a json file that is on a server like this:
[
{
“time”: “15:00”,
“leg”: “P2”,
“price”: “0.05226884830”,
“kwh”: “EUR/kWh”,
“date”: “02-07”
},
{
“time”: “16:00”,
“leg”: “P2”,
“price”: “0.05245115818”,
“kwh”: “EUR/kWh”,
“date”: “02-07”
},…{}]
I have to make a loop to compare the current time with “time” and if they are equal extract the “price” and save price in a helper. Thanks.
Thank you Tron for answering, is the syntax of this template correct?
value_template: >
{{ (value_json
|selectattr(‘time’,‘eq’,(now().time()|string)[:3] ~ ‘00’
|first)[‘price’] }}
it gives me an error:
Invalid config for ‘rest’ from integration ‘sensor’ at sensor.yaml, line 351: invalid template (TemplateSyntaxError: expected token ‘)’, got ‘[’) for dictionary value ‘value_template’, got "{{ (value_json\n |selectattr(‘time’,‘eq’,(now().time()|string)[:3] ~ ‘00’\n |first)[‘price’] }}\
Thanks Troon, it works Ok, the value assigned to: value_json is a string, how can I get a numerical value? If I put:
…|first)[‘price’] | float does not assign any value to value_json.
States are always strings. What are you trying to do?
value_json is the entire object out of your first post. The bits of the template after that select the list item with the correct time and then pull the price out of that.
The sensor state will be a string that looks like a number.
The value returned in value_json is like this: 0.05226884830 but I need a numerical value to include it in general energy as a cost, otherwise it gives me the error:
The entity has a non-numeric status
The following entities have a state that cannot be parsed as a number:
sensor.total_octopus_rate (0.18379928024)
Please post the full config you are using for the sensor, and a screenshot from Developer Tools / States showing its state. Make sure you format the config properly by surrounding with three backticks:
```
CODE HERE
```
Don’t press submit until the preview is formatted like my code snippets above.
perfect, about : Updating every 60s is probably overkill , I would like to update it once every 24 hours, which would be: scan_interval: 86400, but the request to resource: http://…json would have to be at 00:00 h each day, how could I do it? Thank you for your patience.