Apologies, I know this has been asked before, but I can’t find any good examples.
I have the following rest template sensor, which works fine, except when the disk goes to sleep (The rest sensor gives me the output from a S.M.A.R.T test). When this happens, I check the output from the rest call, and change the sensor values, usually to something like zero, but I’d like to keep the last known value instead, how might I go about this ?
The current sensor is below:-
- resource: http://someurl/output.txt
scan_interval: 300
sensor:
- name: main_hdd_mode_restful
value_template: >
{% if "Device is in" in value %}
{{ (value|regex_findall("Device is in .*"))[0].split()[3] }}
{%else%}
Active
{%endif%}
- name: main_hdd_temperature_restful
value_template: >
{% if "Temperature_Celsius" in value %}
{{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
{%else%}
0
{%endif%}
unit_of_measurement: "°C"
- name: main_hdd_power_on_hours_restful
value_template: >
{% if "Power_On_Hours" in value %}
{{ (value|regex_findall("Power_On_Hours.*"))[0].split()[8] }}
{%else%}
0
{%endif%}
unit_of_measurement: "hours"
- name: main_hdd_health_restful
value_template: >
{% if "SMART overall-health self-assessment test result:" in value %}
{{ (value|regex_findall("SMART overall-health self-assessment test result:.*"))[0].split(":")[1] }}
{%else%}
Unknown
{%endif%}
I’ve actually now tried this and changed the template sensor to this:-
- resource: http://192.168.0.202:8000/DriveD.txt
scan_interval: 300 #checks every 5 minutes, but the file is actually only updated every 40 minutes on the host machine.
sensor:
- name: main_hdd_mode_restful
value_template: >
{% if "Device is in" in value %}
{{ (value|regex_findall("Device is in .*"))[0].split()[3] }}
{%else%}
Active
{%endif%}
- name: main_hdd_temperature_restful
value_template: >
{% if "Temperature_Celsius" in value %}
{{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
{%else%}
{{ this.state }}
{%endif%}
unit_of_measurement: "°C"
But the entity is now showing the value ‘unknown’ and this is in the logs:-
Template variable error: 'this' is undefined when rendering '{% if "Temperature_Celsius" in value %} {{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }} {%else%} {{ this.state }} {%endif%}'
The sensors all now look like this:-
(The one I posted the screenshot from is at the bottom)
- resource: http://someurl/output.txt
scan_interval: 300
sensor:
- name: main_hdd_mode_restful
value_template: >
{% if "Device is in" in value %}
{{ (value|regex_findall("Device is in .*"))[0].split()[3] }}
{%else%}
Active
{%endif%}
- name: main_hdd_temperature_restful
value_template: >
{% if "Temperature_Celsius" in value %}
{{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
{%else%}
{{ this.state }}
{%endif%}
unit_of_measurement: "°C"
- name: main_hdd_power_on_hours_restful
value_template: >
{% if "Power_On_Hours" in value %}
{{ (value|regex_findall("Power_On_Hours.*"))[0].split()[8] }}
{%else%}
{{ this.state }}
{%endif%}
unit_of_measurement: "hours"
- name: main_hdd_health_restful
value_template: >
{% if "SMART overall-health self-assessment test result:" in value %}
{{ (value|regex_findall("SMART overall-health self-assessment test result:.*"))[0].split(":")[1] }}
{%else%}
{{ this.state }}
{%endif%}
But all of them are now doing this, where I have replaced a default value with {{this.state}}, I’m now getting unknown, even after a valid state has been ‘seen’.