I am looking to write a template sensor that will ignore sensors that have not updated in X amount of time. I have put a bunch of random peoples code together, so i am sure i messed something up. But, it is not currently working.
- sensor:
- name: "Average Downstairs 433 Temperature"
unique_id: '5e9cd8d7-9516-4dda-b43f-9ebf31ae24cb'
device_class: temperature
state_class: measurement
state: >
{% set t = strptime(((now().timestamp() - 300) | timestamp_utc) ~ '+00:00', '%Y-%m-%d %H:%M:%S%z') %}
{% set sensors = [states.sensor.acurite_dining_room_temp_433,
states.sensor.acurite_living_room_temp_433,
states.sensor.acurite_kitchen_temp_433,
states.sensor.acurite_spare_bedroom_temp_433,
states.sensor.acurite_spare_bathroom_temp_433,
states.sensor.acurite_computer_room_temp_433,
states.sensor.acurite_front_bathroom_temp_433,
states.sensor.acurite_entryway_temp_433,
states.sensor.acurite_pantry_temp_433]
| rejectattr('state', 'in', ['unavailable','unknown',''])
| rejectattr('last_changed', '<', t)
| list %}
{% set qty = sensors | count %}
{% set x = sensors | map(attribute='state') | list | string %}
{% set total = x.replace("'","") | from_json | sum %}
{{ 'unavailable' if qty == 0 else (total/qty) | round (1) }}
unit_of_measurement: °F