How can I set a Sensor's Attribute

Googled this several time, I just cant find out how to set a template sensors attribute from withinn the state template ??

  - unique_id: test
    name: "Test"
    state: >-
      {% set result = namespace(sensors = []) %}
      {% if states('switch.wiser_away_mode') == "on" %}
        {% for state in states.climate | rejectattr('attributes.preset_mode', 'undefined') | selectattr('attributes.preset_mode', 'eq', 'Away Mode') -%} 
          {% if state.attributes.current_temperature|float < states('number.wiser_away_mode_target_temperature')|float %}
            {% set result.sensors = result.sensors + [(state.attributes.friendly_name,state.attributes.current_temperature)] %}
          {% endif %}
        {% endfor -%}
      {% endif %}
      {{ result.sensors }}
      {% set this.attributes.count = 1 %}
    
    attributes:
      count: 0

Have you tried creating a scene (on the fly) and then reloading that?

I don’t think you can. You have to put a template under the “attributes:” entry as in this example. Therefore you have to duplicate some of the code as they have done in the example.

      {% set this.attributes.count = 1 %}

Even if that would work the count attribute would always be 1.

That is the same way I do it in some cases:

sensor:    
  - platform: template
    sensors:
      test:
        friendly_name: test
        value_template: >
          {% set result = expand(states.climate)
            | rejectattr('attributes.preset_mode', 'undefined') 
            | selectattr('attributes.preset_mode', 'eq', 'Away Mode')
            | selectattr('attributes.current_temperature', 'lt', states('number.wiser_away_mode_target_temperature')|float(0))
            | map(attribute='name')
            | list %}
            
          {{ result }}
  
        attribute_templates:
          count: >
            {% set count = expand(states.climate)
              | rejectattr('attributes.preset_mode', 'undefined') 
              | selectattr('attributes.preset_mode', 'eq', 'Away Mode')
              | selectattr('attributes.current_temperature', 'lt', states('number.wiser_away_mode_target_temperature')|float(0))
              | map(attribute='name')
              | list
              | length %}
      
            {{ count }}
2 Likes

If you don’t mind the occasional error message or default value due to load order, you can take advantage of the fact that attributes can be data type other than strings and use self-referencing…

- name: "Test"
  state: "{{  (this.attributes.count > 0) | default("error", 1) }}"
  attributes:
    list: >-
      {% set result = namespace(sensors = []) %}
      {% if states('switch.wiser_away_mode') == "on" %}
        {% for state in states.climate 
        | rejectattr('attributes.preset_mode', 'undefined') 
        | selectattr('attributes.preset_mode', 'eq', 'Away Mode') -%} 
          {% if state.attributes.current_temperature | float(0) < 
          states('number.wiser_away_mode_target_temperature') | float(0) %}
            {% set result.sensors = result.sensors + 
            [ (state.attributes.friendly_name, state.attributes.current_temperature) ] %}
          {% endif %}
        {% endfor -%}
      {% endif %}
      {{ result.sensors }}
    count: "{{  this.attributes.list | count | default(0, 1) }}"