Templating from sensor entity

I have a commandline sensor that runs a python script. This script returns a json array. My plan was to create other sensors with a template that retrieve values from the state of the commandline sensor.

The commandline sensor output is:
{"berkman": {"euro95": "1.610", "diesel": "1.310"}, "shell": {"euro95": "1.650", "diesel": "1.390"}, "tamoil": {"euro95": "1.640", "diesel": "1.370"}}

When I enter the folowing in het home assistant template ui it gives a “expected token ‘end of print statement’, got ‘[’” error.

{{ states.sensor.brandstof_prijzen | value_json['berkman']['euro95'] }}

What am I doing wrong?

value_json is not available in a typical template as a filter. Try it simpler:

{% set my_test_json = {"berkman": {"euro95": "1.610", "diesel": "1.310"}, "shell": {"euro95": "1.650", "diesel": "1.390"}, "tamoil": {"euro95": "1.640", "diesel": "1.370"}}
 %}

{{ my_test_json.berkman.diesel }}

How would I put this in a template sensor?

And you are using the static json value but I want to use the dynamic state op the commandline sensor.

Depending on the context of where you’re defining it, I think you can just remove the states.sensor.brandstof_prijzen | and use only value_json['berkman']['euro95'] or value_json.berkman.euro95

value_json is not a filter. value_json is an automatically created variable when in a value_template definiton.

The problem I have is when i create a sensor that runs a script, the script calls a web page. I don’t want to make a request to the webpage for every type of fuel and every fuelstation.

No I have 1 sensor that calls the script and puts the json array in the state of the command line sensor. See image.

From that state I want to create multiple sensors that use that information.

I think I understand what you mean. You probably want something like the following.

Notes:

  • I’m use the rest sensor but the command-line sensor works the same way. The service referenced below is a real service, you can use the below directly and it should work.
  • I am using different approaches to create the 4 sensors below. Each sensor is created slightly differently, but ultimately they are essentially the same. So check each sensor carefully to see the differences, and use whichever approach you prefer in your real implementation.
sensor:
  # If your 'results' are at top-level, then you have to manually specify each key
  - platform: rest
    name: "top"
    resource: http://my-json-server.typicode.com/tonyapuzzo/fake/example
    force_update: no
    headers:
        User-Agent: Home Assistant
        Content-Type: application/json
    json_attributes: ['berkman', 'shell', 'tamoil']

  # But if your results are nested, then you can dereference the top-level
  - platform: rest
    name: "nested"
    resource: http://my-json-server.typicode.com/tonyapuzzo/fake/db
    force_update: no
    headers:
        User-Agent: Home Assistant
        Content-Type: application/json
    json_attributes: ['example']

  - platform: template
    sensors:
      berkman_euro95:
        unit_of_measurement: '€'
        value_template: >-
          {{- states.sensor.top.attributes.berkman.euro95 -}}
      shell_euro95:
        unit_of_measurement: '€'
        value_template: >-
          {{- state_attr('sensor.top', 'shell').euro95 -}}
      berkman_diesel:
        unit_of_measurement: '€'
        value_template: >-
          {{- states.sensor.nested.attributes.example.berkman.diesel -}}
      shell_diesel:
        unit_of_measurement: '€'
        value_template: >-
          {{- state_attr('sensor.nested', 'example').shell.diesel -}}
          
1 Like

I’m not aware of a way to dynamically create sensors, but you can use a template to generate the sensors. Paste this into the templates dev tool and then you can copy/paste the result into the config:

  - platform: template
    sensors:
{%- for vendor in state_attr('sensor.nested', 'example') %}
    {%- for fuel in state_attr('sensor.nested', 'example')[vendor] %}
    {{ vendor ~ '_' ~ fuel }}:
        unit_of_measurement: '€'
        value_template: {{ "'{{" }} state_attr("sensor.nested", "example")[vendor][fuel] {{ "}}'" }}
    {%- endfor %}
{%- endfor %}
1 Like

I am going to try! The first differance I see that I did wrong is the json_attributes. I did not use square brackets when testing, maybe that will help.