How to improve sensor?

Hi all,

I have the following working sensor (value_template), but I learned that it should be something with is_state_attr instead of states.sensor.

I just did not figure out the correct syntax for doing so.

  - platform: rest
    resource: http://some.resource.ip.xy
    scan_interval: 10
    name: "sensor_name"
    value_template: "{{ value_json['system.system_info'] }}"
    json_attributes:
      - system
  - platform: template
    sensors:
      oekofen_aussentemp:
        friendly_name: 'some_friendly_name'
        value_template: '{{ (((states.sensor.sensor_name.attributes["system"]["L_ambient"]|int) *0.1) | round(1) )}}'
        unit_of_measurement: "°C"

During startup I get this error message in the log but after some seconds the value is showing up.

Could not render template some_friendly_name: UndefinedError: 'mappingproxy object' has no attribute 'system'

I’ve read https://www.home-assistant.io/docs/configuration/templating/ up and down and other entries in the forum but this is the only working format I can craft.

Help highly appreciated :slight_smile:
Ralf

Are you trying to check if that entity’s state attribute called system is L_ambient? If so…

"{{ is_state_attr('sensor.name', 'system', 'L_ambient') }}"

But that appears to be a template sensor, so that wouldn’t work (unless it were a binary template sensor and you want that to represent on).

I think the attribute “system” is a dictionary itself and he wants to extract “L_ambient” from this dict, multiply by 0.1 and round it.

Don’t know if this works, but try this:

value_template: '{{ (state_attr('sensor_name', 'system')["L_ambient"] | int) *0.1) | round(1) }}'

1 Like

Ah, I didn’t scroll over far enough apparently lol

Should my template work? Can I extract a value from an attribute dictionary like this?

Burningstone, you are right. “system” is a dictionary itself"

Error: Testing configuration at /config
ERROR:homeassistant.util.yaml.loader:while parsing a block mapping
  in "/config/configuration.yaml", line 186, column 9
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 187, column 42
Failed config
  General Errors: 
    - Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/configuration.yaml", line 186, column 9
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 187, column 42

Line 187 is the one with the value_template

Color coding in ide 9 is also showing that some " ’ … are wrong

Not sure, can’t say I’ve done that before. @RalfP can you post a screenshot of the state attributes of that entity in Developer Tools > States? Something like this:

This is the “full” sensor

this is the template sensor

To get the value of 11.6 just divide the 116 by 10

Ah. It looks like this section of the templating docs might be relevant here: https://www.home-assistant.io/docs/configuration/templating/#processing-incoming-data

I believe the value_template would be something like

"{{ value_json['system']['L_ambient'] | int * 0.1 }}"

You’ll need to set the entity_id variable in the template sensor for that to work, otherwise Home Assistant won’t know what you’re referring to with that template.

I honestly have no idea if this will work since I’ve never done it before. I’m just hoping my interpretation of the docs along with the knowledge I do have about Home Assistant is correct here.

in the part ENTITY_ID THAT BEGINS WITH A NUMBER in https://www.home-assistant.io/docs/configuration/templating/#processing-incoming-data
the example is also like states.device_tracker['2008_gmc'] but this is not a nested dictionary :confused:

{{ state_attr('sensor.name', 'system') ['L_ambient'] }}

first see if this gets you the number
if so, then simply add the calculation of your liking

check here for reference: [SOLVED] Parsing a json value from an existing entity in a template sensor

1 Like

So you have this config and get the error you posted?

- platform: template
    sensors:
      oekofen_aussentemp:
        friendly_name: "some_friendly_name"
        value_template: '{{ (state_attr('sensor_name', 'system')["L_ambient"] | int) *0.1 | round(1) }}'
        unit_of_measurement: "°C"

The problem is the usage of the quotes

'{{ state_attr("oekofen", "system") ["L_ambient"] }}'

is looking good in the editor but throwing errors

Using your way {{ state_attr('sensor.name', 'system') ["L_ambient"] }} is missing the quotes and thus not working

Error: Testing configuration at /config
ERROR:homeassistant.util.yaml.loader:while parsing a block mapping
  in "/config/configuration.yaml", line 186, column 9
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 187, column 42
Failed config
  General Errors: 
    - Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/configuration.yaml", line 186, column 9
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 187, column 42

You are mixing quotes

The general rule should always be applied - double on the outside, single EVERYWHERE ELSE
I’m not shouting honest :smile:

You are right, Mutt. Question is: what would be the right quotes for nested dictionary parsing?

value_template: >
  {{ state_attr('sensor.name', 'system') ['L_ambient'] }}

use multi-line and dispense with the quoting issues
(sorry for the mixup of quotes in my example above, corrected them)

2 Likes

this version is throwing other errors but again messages from checking the config is more than one screen, so difficult to get to the firts line of the error

be sure to copy correctly, and not from the community posts. I can see incorrect curly quotes in your post, which are not accepted. Always type you own quotes…
what does the template show in the template editor?

1 Like

You are right, sorry for that. What about this:

- platform: template
    sensors:
      oekofen_aussentemp:
        friendly_name: "some_friendly_name"
        value_template: "{{ (state_attr('sensor.sensor_name', 'system')['L_ambient'] | int) *0.1 | round(1) }}"
        unit_of_measurement: "°C"