History graph and attribute of sensors

Hello, I’m trying to create a template sensor from an attribute of a sensor to place it on an history graph. I’ve seen there are several topics but I can’t make it work.

I have a sensor based on a rest call:

  • platform: rest
    name: xxxx
    resource: xxxxxxxxxxxxx
    timeout: 15
    scan_interval: 60
    json_attributes:
    • status
    • viewers
      value_template: ‘OK’

And that seems fine.

Now I want to make a sensor with the status that can assume different values to plot it on a history graph.

I tried to add this yaml to the template file but it doesn’t work:

  • sensor:
    xxxx_online:
    friendly_name: “xxxxxx”
    state: >
    {{ state_attr(‘sensor.xxxx’,‘status’) }}

How can I make a correct sensor template that display the value of status attribute?

Please post your yaml properly.

In sensor yaml file:

- platform: rest
  name: testcam
  resource: https://xxxxxx
  timeout: 15
  scan_interval: 60
  json_attributes:
    - status
    - viewers
  value_template: 'OK'

In template yaml file:

  - sensor:
      testcam_online:
        friendly_name: "online_status"
        state: >
          {{ state_attr('sensor.testcam','status') }}

Sensor seems to work ok, extracting json attributes. Template sensor desn’t seem to work. The status is a text that can assume different values and I would like to make a sensor to use it in history graph.

If I try

          {{ state_attr('sensor.testcam','status') }}

in the template editor it seems ok, it return for example offline, but no sensor is created if I look up for testcam_online

Try this.

- sensor:
    - name: online_status
      unique_id: testcam_online
      state: >-
        {{ state_attr('sensor.testcam','status')  }}

You could also put this in your sensor yaml file.

- platform: template
  sensors:
    testcam_online:
      value_template: >-
        {{ state_attr('sensor.testcam','status') }}
      friendly_name: online_status     

It seems to work with the first suggestion.
I am not sure if I was missing the - after state: > or if the unique_id is mandatory

Thank you

1 Like