Automation with file json, Help

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.

You’re jumping ahead to implementation in your problem description: there’s no need for a loop.

Please post code correctly formatted. I would have liked to have used your example JSON to test, but it’s too fiddly with all the “smart” quotes.

You need a RESTful integration sensor like this:

rest:
  - resource: YOUR_URL
    scan_interval: 86400
    sensor:
      - name: "Current energy price"
        value_template: >
          {{ (value_json
              |selectattr('time','eq',(now().time()|string)[:3] ~ '00')
              |first)['price'] }}
        unit_of_measurement: 'EUR/kWh'

Then an automation to update it at the top of each hour:

trigger:
  - platform: time_pattern
    minutes: 0
action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.current_energy_price

This doesn’t use the date in the JSON. That’s easy enough to add if needed.

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’] }}\

That config doesn’t go into sensors.yaml. It goes straight into configuration.yaml with rest: at the top level.

But you’re right, there was a typo, now fixed above.

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)

Thanks

That is a numerical value?!

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.

- platform: rest
      resource: http://192.168.1.105:8080/homeassistant/octopus.json 
      scan_interval: 60
      name: tarifa_octopus_total
      value_template: >
        {{ (value_json
            |selectattr('hora','eq',(now().time()|string)[:3] ~ '00')
            |first)['precio'] }}

the file json is:

[
  {
    "hora": "15:00",
    "tramo": "P2",
    "precio": "0,05226884830",
    "kwh": "EUR/kWh",
    "fecha": "02-07"
  },
  {
    "hora": "16:00",
    "tramo": "P2",
    "precio": "0,05245115818",
    "kwh": "EUR/kWh",
    "fecha": "02-07"
  },
  {
    "hora": "17:00",
    "tramo": "P2",
    "precio": "0,05941308806",
    "kwh": "EUR/kWh",
    "fecha": "02-07"
  },......{....}]

image

Ok, I understand now. Please don’t try to translate data and error messages.

Your state has a European-style “decimal comma”, which does not convert to a number.

Use this:

    - platform: rest
      resource: http://192.168.1.105:8080/homeassistant/octopus.json 
      scan_interval: 60
      name: tarifa_octopus_total
      value_template: >
        {{ (value_json
            |selectattr('hora','eq',(now().time()|string)[:3] ~ '00')
            |first)['precio']|replace(',','.') }}

Updating every 60s is probably overkill.

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.

See the automation in my first post. That updates at xx:00 every hour. For every day, use this automation:

trigger:
  - platform: time
    at: "00:00"
action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.tarifa_octopus_total

Might be better at “00:05” to ensure the new data is available.

Thank you very much for everything and greetings. Everything Ok

1 Like