RESTful sensor name_template

I tried to enter name_template: in rest sensor.
But it gives error that name_template: is not supported, but here RESTful Sensor - Home Assistant it states that it should be supported.
In configuration.yaml i have line rest: !include configurations/rest.yaml
and in rest.yaml i have

  - resource_template: >-
        {% if states("device_tracker.innosilicontechnology_ltd") == 'home' %}
            http://192.168.0.46/cgi-bin/api.py
        {% else %}
            http://192.168.0.143:8123/local/community/innosilicon/api.json
        {% endif %}
    headers:
        Content-Type: application/json
    scan_interval: 60
    timeout: 180
    method: GET
    sensor:
## DEVS ##
      - name: "Innosilicon A6 HB0"
        unique_id: unique_id_innosilicon_a6_hb0
        unit_of_measurement: "MH/s"
        state_class: "measurement"
        value_template: >-
            {% if value_json.DEVS[0]['MHS 5s'] is defined %}
                {{ value_json.DEVS[0]['MHS 5s'] }}
            {% else %}
                {{ this.state }}
            {% endif %}

What i would like to have, is to sensor to change its name depending on JSON message.
I think it should be like this name_template: "Innosilicon A6 HB{{ value_json.DEVS[0]['ASC'] }}" but it is not accepted.
Do i need to move my code under sensor: instead of rest: ?

According to the docs name is a template already. So don’t use name_template, just name.

1 Like

I see, now it is accepted, but it does not work, sensor goes unavailable.
If i put it in template editor it works as expected, but in rest sensor, it does not work.
I have tried
name: "Innosilicon A6 HB{{ value_json.DEVS[0]['ASC'] }}"
name: {{"Innosilicon A6 HB"}}{{ value_json.DEVS[1]['ASC'] }}

What happens if you check if it is defined? Maybe the initialization of the sensor fails because it is done before the rest call is done. It might also be the template is evaluated at multiple other times when the json variable is not there. Maybe store the value you want in an attribute and let the name expand the attribute, so the data is always there?

That did it, i added {% if value_json is defined %} and it started to work.
Thank you.

1 Like