Sensor template with multiple attributes

Hi guys!
I am currently struggeling to create a sensor that returns two calculated integer values as attributes.
This is a simplified example code for the sensor:

  - sensor:
    - name: "Leistungslimit OW"
      unit_of_measurement: "W"
      state: >
          {{ is_state('binary_sensor.gh_ost_erreichbar', 'on') }}
      attributes:
        ost: >
          {% set max = 200 | int %}
          {% set min = 100 | int %}
          {% set limit1 = max + min %}
          {% set limit2 = max - min %}
          {{ limit1 }}
        west: >
          {{ limit2 }}

I am able to access the ‘ost’ attribute with:
{{ state_attr('sensor.leistungslimit_ow', 'ost') }}

Unfortunately the ‘west’ attribute doesn’t return a value.
I assume that variables are only valid inside an attribute and cannot be accessed in another one? Is there any way to pass them through? I want to avoid doing the calculations in both attributes.

Thanks for any help you can provide!

I think you need to include all the code in each attribute:

  - sensor:
    - name: "Leistungslimit OW"
      unit_of_measurement: "W"
      state: >
          {{ is_state('binary_sensor.gh_ost_erreichbar', 'on') }}
      attributes:
        ost: >
          {% set max = 200 | int %}
          {% set min = 100 | int %}
          {% set limit1 = max + min %}
          {{ limit1 }}
        west: >
          {% set max = 200 | int %}
          {% set min = 100 | int %}
          {% set limit2 = max - min %}
          {{ limit2 }}

Hi templeton_nash!

If I read the current sensor values in each attribute and than compute the results (like in the following example):

- sensor:
    - name: "Leistungslimit OW"
      unit_of_measurement: "W"
      state: >
          {{ is_state('binary_sensor.gh_ost_erreichbar', 'on') }}
      attributes:
        ost: >
          {% set pwr_zaehler = states('sensor.zaehler_momentanleistung') %}
          {% set pwr_solar = states('sensor.solarleistung') | int %} 
          {% if is_number(pwr_zaehler) and int(pwr_zaehler) >= pwr_solar %}
            {% set pwr_limit_ost = pwr_limit_max %}
            {% set pwr_limit_west = pwr_limit_max %}

            .............

          {{ pwr_limit_ost }}

        west: >
          {% set pwr_zaehler = states('sensor.zaehler_momentanleistung') %}
          {% set pwr_solar = states('sensor.solarleistung') | int %} 
          {% if is_number(pwr_zaehler) and int(pwr_zaehler) >= pwr_solar %}
            {% set pwr_limit_ost = pwr_limit_max %}
            {% set pwr_limit_west = pwr_limit_max %}

             .............

          {{ pwr_limit_west }}

Can I expect that the sensor values haven’t changend during the reads? Or is it possible that the reading is different for each attribute?

Thanks!

Sorry, I don’t know the answer to this.

I want to calculate the values every ten seconds.
Will it help if I use a trigger-based template to make sure that the values don’t change during execution of the attributes?

Like this:

  - trigger:
      - platform: time_pattern
        seconds: "/10"
    sensor:
      - name: "Leistungslimit OW"
        unit_of_measurement: "W"
        state: >
            {{ is_state('binary_sensor.gh_ost_erreichbar', 'on') }}
        attributes:
          ost: >
            .......
          west: >
            .......

If you want to be sure both attributes are working with the same values, you can assign the value to variables at the trigger.

  - trigger:
      - platform: time_pattern
        seconds: "/10"
        variables:
          max: "{{ states('sensor.example_1') | int }}"
          min: "{{ states('sensor.example_2') | int }}"
    sensor:
      - name: "Leistungslimit OW"
        unit_of_measurement: "W"
        state: >
            {{ is_state('binary_sensor.gh_ost_erreichbar', 'on') }}
        attributes:
          ost: "{{ max + min }}"
          west: "{{ max - min }}"
1 Like

I have just tested your suggested code (a bit simplified) in the development tools. Unfortunately it seems like the variables are not valid/defined in the attribute block:

- trigger:
      - platform: time_pattern
        seconds: "/10"
        variables:
          maximum: "{{ states('sensor.zaehler_momentanleistung') | int }}"
    sensor:
      - name: "test"
        unit_of_measurement: "W"
        state: >
            {{ is_state('binary_sensor.gartenhaus_ost_erreichbar', 'on') }}
        attributes:
          ost: "{{ maximum }}"

It results in a warning: “‘maximum’ is undefined” and ost attribute does not read the expected value :cry:

  • trigger:
    - platform: time_pattern
    seconds: “/10”
    variables:
    maximum: “377”
    sensor:
    - name: “test”
    unit_of_measurement: “W”
    state: >
    True
    attributes:
    ost: “”

The template editor tool does not understand YAML, so testing YAML script variables there will never work without additional Jinja set statements. The method is sound, but you must actually configure the sensor.

image

image

1 Like

I wasn’t aware of this limitation, good to know!
After adding the code to my configurartion.yaml it works as expected. :slightly_smiling_face:
That’s exactly what I was looking for.

Thanks for your help! :metal: