Template to only update one sensor

I want to track 2 weight ranges from a single sensor.

I have a template that checks the weight and if above a set level updates one sensor and if below updates a second.

The problem is, both sensors always get updated and I only want the one that needs to be updated, updated (it actually shows as changed and updated).

- platform: template
  sensors:
    weight_high:
      value_template: >-
        {% set weight = states('sensor.ble_weight') | float %}
          {% if 7 <= weight %}
            {{ states('sensor.ble_weight') }}
          {% else %}
            {{ states('sensor.weight_high') }}
          {% endif %}

and a second template sensor if below 7.

If set to the previous value (I tried without the else), I was hoping the last_changed would not update.

Is there any way to ā€˜abandonā€™ the template processing so the state is not updated?

Use trigger-based template sensors instead of state-based.

Whatā€™s the reason for that? To keep track of the exact date and time when the value exceeded 7?

Because on startup itā€™ll get set to the current date and time anyway.

@123 No, just I thought if the entity state value was changed, last_changed would not get updated, but last_updated would (I have never been absolutely clear on the difference though).

@Didgeridrew thanks - not ventured into these before.

template:
  - trigger:
    - platform: template
      value_template: >-
        {% set weight = states('sensor.ble_weight') | float %}
          {% if 70 <= weight %}
            true
          {% endif %}
  - sensor:
    - name: weight_high
      state: "{% states('sensor.ble_weight') | float %}"

First attempt failed :frowning:

Iā€™ll have another go tomorrow, but any suggestions welcome :grinning:.

I think this will pass Check Configuration:

template:
  - trigger:
      - platform: template
        value_template: "{{ states('sensor.ble_weight') | float(0) >= 70 }}"
    sensor:
      - unique_id: weight_high
        name: "Weight High"
        state: "{{ states('sensor.ble_weight') }}"

However, I donā€™t believe itā€™s the solution. For example, after you create it, its value will be unknown until ble_weight exceeds 70. The moment you Reload Template Entities or restart Home Assistant, it will go back to unknown.

If you attempt to compensate by adding additional triggers to handle reload/restarts, it introduces another problem.

template:
  - trigger:
      - platform: template
        value_template: "{{ states('sensor.ble_weight') | float(0) >= 70 }}"
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - unique_id: weight_high
        name: "Weight High"
        state: "{{ states('sensor.ble_weight') }}"

On restart/reload it will simply set the Template Sensorā€™s value to the current ble_weight (and refresh last_updated/last_changed).

If you attempt to mitigate that behavior by enhancing the state template with a test to check if ble_weight is >= 70, then youā€™re right back to the where you were with your original Template Sensor.

ā€¦ but donā€™t let me stop you from trying it; maybe Iā€™m wrong about how itā€™ll work. :slightly_smiling_face:

You are correct, but that is no worse than it is currently as the base sensor reports unknown on restart for a period.

Thanks for the suggestion, it works (of course). However, how do I add the second sensor? Duplicating that and changing the sensor names, complains of a duplicate label in VSC.

template:
  - trigger:
    - platform: template
      value_template: "{{ states('sensor.ble_weight_5ccad3755549') | float(0) >= 70 }}"
  - sensor:
    - unique_id: weight_high
      state: "{{ states('sensor.ble_weight_5ccad3755549') }}"

template:
  - trigger:
    - platform: template
      value_template: "{{ states('sensor.ble_weight_5ccad3755549') | float(0) < 70 }}"
  - sensor:
    - unique_id: weight_low
      state: "{{ states('sensor.ble_weight_5ccad3755549') }}"

I can see the documentation says You can define multiple configuration blocks as a list. but TBH Iā€™m not sure how to do that and perhaps an example of this would help :confused:.

Also, I have a number of sensor templates, getting to the bottom of the documentation (looking for multiple sensor examples) I see that this format seems to be deprecated (in my sensors.yaml - this also has the unknown at start problem of course).

- platform: template
  sensors:
    s_cert_exiry_d:
      friendly_name: 'Cert Expiry'
      value_template: >
        {{ ((as_timestamp(states('sensor.cert_expiry_timestamp_hassio_borpin_net')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}
      unit_of_measurement: 'days'
  • I donā€™t use the templates enough to really et my head around the specific syntax :frowning:

This is the whole point of the default in the other thread, and the entire reason default was enforced. So that you could catch unknowns.

template:
  - trigger:
    - platform: template
      value_template: "{{ states('sensor.ble_weight_5ccad3755549') | float(0) >= 70 }}"
  - sensor:
    - unique_id: weight_high
      state: "{{ states('sensor.ble_weight_5ccad3755549') }}"
      availability: "{{ states('sensor.ble_weight_5ccad3755549') | float(none) is not none }}"

Just keep in mind that using a trigger like that will only cause it to update when it crosses the threshold. It will not constantly update.

1 Like

Ah, thatā€™s not what I want. I want it to process any time the base sensor updates.

I keep forgetting that availability was added to all Template entities recently (mostly in the September release) including this bug-fix for Trigger-based ones. On some rainy day, I should review all of my Template entities and enhance them with availability.

1 Like

Didnā€™t even know about that bug

Iā€™m thinking that I need to set the state via an automation rather than a template as I need a trigger (base sensor updated) and a condition (greater than the threshold).

Definitely a job for Node-Red then.

For future reference, Iā€™d still like to understand the syntax for specifying more than one trigger/sensor pairing.

Yeah, thereā€™s that too; standard behavior for Template Trigger and Numeric State Trigger (ā€˜latchingā€™ that must be reset before it will trigger again).

Quick and dirty method is create an automation with a State Trigger monitoring the ble_weight sensor. Use a Template Condition confirming trigger.to_state.state is >= 70 (or a Numeric State Condition). The action is a service call storing trigger.to_state.state to a Helper like an input_number or input_text. The Helperā€™s value survives restarts and itā€™s updated/changed only when the sensorā€™s value is above 70. Only drawback is itā€™s a Helper, not a sensor.

If it has to be a sensor, it can be done with an MQTT Sensor (assuming you already use MQTT for other purposes, otherwise you would need to set it up first).


EDIT

Just read what you posted while I was typing furiously ā€¦ :slightly_smiling_face: Looks like we have the same idea about an automation.

1 Like

@123 thanks :slight_smile:

Re the bit about multiple sensors?

Four Trigger-based Template Sensors, all triggered by the same Time Trigger. The first two are sensors and the other two are binary_sensors.

template:
  - trigger:
      - platform: time
        at: input_datetime.whatever
    sensor:
      - name: 'cat'
        state: '{{ ... cat template... }}'
      - name: 'dog'
        state: '{{ ... dog template... }}'
    binary_sensor:
      - name: 'up'
        state: '{{ ... up template... }}'
      - name: 'down'
        state: '{{ ... down template.. }}'
1 Like

What if a different trigger? VSC complained about 2 template: entries. Is that a bug with the VSC extension? TBH I did not check the config.

You need to leave off the second ā€œtemplate:ā€ in you configā€¦ and remove the hyphens in front of ā€œsensor:ā€

template:
  - trigger:
    - platform: template
      value_template: "{{ states('sensor.ble_weight_5ccad3755549') | float(0) >= 70 }}"
    sensor:
    - unique_id: weight_high
      state: "{{ states('sensor.ble_weight_5ccad3755549') }}"

  - trigger:
    - platform: template
      value_template: "{{ states('sensor.ble_weight_5ccad3755549') | float(0) < 70 }}"
    sensor:
    - unique_id: weight_low
      state: "{{ states('sensor.ble_weight_5ccad3755549') }}"
1 Like
template:
  - trigger:
      - platform: time
        at: input_datetime.whatever
    sensor:
      - name: 'cat'
        state: '{{ ... cat template... }}'
      - name: 'dog'
        state: '{{ ... dog template... }}'
    binary_sensor:
      - name: 'up'
        state: '{{ ... up template... }}'
      - name: 'down'
        state: '{{ ... down template.. }}'
  - trigger:
      - platform: state
        entity_id: binary_sensor.something
        to: 'on'
    sensor:
      - name: 'fish'
        state: '{{ ... fish template... }}'

This example shows Trigger-based sensor and binary_sensor entities but you can also define standard non-trigger-based sensors, binary_sensors, and numbers. See the documentation for examples.

1 Like