Hello,
I’ve got a ‘OpenWeatherMap Forecast Precipitation’ sensor. It works fine, however sometimes, when there is no precipitation at all, it shows the state “unavailable” or “unknown”. I count the sum for next 12 hours by the “statistics” template from this sensor. I use it to check if should I water the garden if it exceeds the threshold.
But how can I change “unavailable” or “unknown” state for simple 0.0 value? I tried value_template, but it does not work in statistics template.
sensor:
- platform: statistics
name: "Precipitation in next 12 hours"
entity_id: sensor.openweathermap_forecast_precipitation
state_characteristic: sum
max_age:
hours: 12
You can create a template sensor to filter your statistics sensor.
template:
- sensor:
- name: "Filtered Precipitation in last 12 hours"
unit_of_measurement: "mm"
device_class: precipitation
state_class: measurement ### only include this line if you want long term statistics.
state: "{{ states('sensor.precipitation_in_next_12_hours') if states('sensor.precipitation_in_next_12_hours')|is_number else 0 }}"
Whoah! That’s a prompt message! Thanks! It looks that it does what I wanted.
But can you explain the syntax further? I understand the ending "|is_number else 0 (that’s quite obvious), but the beginning looks odd…?
Is the first part a declaration and then the condition “if…”?
state: "{{ states('sensor.precipitation_in_next_12_hours') if states('sensor.precipitation_in_next_12_hours')|is_number else 0 }}"