Sensor template value empty

Via platform rest i get the answer:

{"sensors":[{"id":"283b7f","lvl":59.7,"pct":73,"bat":100,"lstRead":"2020-12-07T20:26:07+01:00","lstEmpty":"2020-12-02T10:22:25+01:00","daysLeft":5}]}

configuration.yaml

  - platform: rest
    name: test
    json_attributes:
      - lvl
      - pct

    resource: http://***.***.***.***/state
    value_template: '{{ value_json.sensors }}'
  - platform: template
    sensors:
      lvl:
        friendly_name: 'name1'
        value_template: '{{ states.sensor.json_sensors.attributes["lvl"] }}'
      pct:
        friendly_name: 'name2'
        value_template: '{{ states.sensor.json_sensors.attributes["pct"] }}'

returns the answer state unavailable.
what am I doing wrong?

Why are you using json_sensors in your value templates for the lvl and pct? The sensor you create is called sensor.test because you choose name: test in the rest sensor.
Also to avoid errors use the notation state_attr('sensor.xxx', 'attribute') instead of states.sensor.xxx.attributes.attribute, see here for more details.

Try this:

  - platform: template
    sensors:
      lvl:
        friendly_name: 'name1'
        value_template: "{{ state_attr('sensor.test', 'lvl' }}"
      pct:
        friendly_name: 'name2'
        value_template: "{{ state_attr('sensor.test', 'pct' }}"

For the purposes of publishing the post, I forgot to rename the sensor.
I tried both of solution (you forgot the sign ) ).
The sensors are available, but their states are empty

sensors is a list, and you’re missing the json_attributes path which gets you to the 1st object in the list (0th item).

  - platform: rest
    name: test
    resource: http://***.***.***.***/state
    value_template: '{{ value_json.sensors[0].id }}'
    json_attributes_path: "$.sensors.0"
    json_attributes:
      - lvl
      - pct
  - platform: template
    sensors:
      lvl:
        friendly_name: 'name1'
        value_template: "{{ state_attr('sensor.json_sensors', 'lvl') }}"
      pct:
        friendly_name: 'name2'
        value_template: "{{ state_attr('sensor.json_sensors', 'pct') }}"
1 Like

Better :slight_smile:
Now sensor ‘test’ has a states atributes

lvl: 58.48
pct: 73
friendly_name: kibelek

but sensors lvl and pct have unknown state

change the entity_id from 'sensor.json_sensors' to 'sensor.test'.

the same - sensors lvl and pct have unknown state

  - platform: rest
    name: test
    resource: http://***.***.***.***/state
    value_template: '{{ value_json.sensors[0].id }}'
    json_attributes_path: "$.sensors.0"
    json_attributes:
      - lvl
      - pct

  - platform: template
    sensors:
      lvl:
        friendly_name: 'Level'
        value_template: "{{ state_attr('sensor.json_test', 'lvl') }}"
      pct:
        friendly_name: 'Percent'
        value_template: "{{ state_attr('sensor.json_test', 'pct') }}"

:man_facepalming:

2 Likes

As I pointed out in the very first reply :man_facepalming:

1 Like

Sorry, my mistake - shame on me!
Working fine - thanks @all