Last state of sensor

Hi guys

I would like to see how long ago a sensor has shown a particular state on a weather sensor.

So i have a AccuWeather weather sensor, and would like to know then the state: “rainy” is last changed.

So e.g. it’s been 2 days since it rained.

Something like the following as a templated sensor should get you there:

    time_since_weather_changed:
      friendly_name: Time since last change
      value_template: "{{ ((as_timestamp(now()) -
                            as_timestamp(states.domain.entity_id.last_changed))/86400)|round(2) }}"

The domain.entity_id needs to be replaced with your weather entity id that you are using. The timestamps are in seconds, so the 86400 converts it to days.

Do you want to know how long any weather state has remained unchanged (that’s what KSC’s example does) or specifically how long it has been since it rained?

Good point, Taras -

@DkAutomater - I don’t think last_changed works with attributes (at least I don’t recall having tried it). Might have to create a new template sensor that takes on the value of the desired attribute and then track that.

Thank you guys, its working :slight_smile:

Wait, no i dont see it.
Where should i put this template in?

How does the template sensor work?

In sensors.yaml.

After that and reloading you will have a new sensor: sensor.time_since_etc…

If your goal is to track the last time it rained wouldn’t it be easier to just have a template binary sensor like this:

template:
  - binary_sensor:
      - name: Is Raining
        state: "{{ is_state('sensor.current_weather', 'rainy') }}"

Then you’ll have a new entity binary_sensor.is_raining which will have state on when its raining and off when its not. When the sensor has state off its last_changed value will be equal to the last time it rained, you can display that value in the UI or use it in an automation as you wish.

Specifically how long it has been since it rained, so i want an automation that run, if its not have been raining for 2 days.

Where should i put this part in?
In the sensors,yaml or binary_sensor.yaml file?

To my knowledge last_changed will not survive a HA restart and thus reset to restart time. I use two different scenarios, store in automation upon rain-off trigger time-date under service: input_datetime.set_datetime with entity_id input_datetime.last_rain or using automation action:

    - service: mqtt.publish
      data:
        topic: "mqtt/rain"
        payload_template: "{{ {{ as_timestamp(states.binary_is_raing.last_changed) }} }}"
        retain: true

Hope this helps!

Sure
I will found it, next time its raining :slight_smile:

Neither? It goes under template: in configuration.yaml, see doc . If you want to break that into a separate file then feel free, there are lots of ways to split up the configuration so which file you put it in depends on which technique you used.

Fair point. If that’s needed then you will need a different approach. Mqtt sounds good.

I got this working in my binary_sensor.yaml file

  • platform: template
    sensors:
    raining_weather:
    friendly_name: “Is it raining”
    device_class: moisture
    value_template: >-
    {{ is_state(‘weather.home’, ‘rainy’)
    or ( states(‘weather.home’) | float > 60 ) }}

Now its in my notify automation:

  • id: notify_no_raining_for_2days
    alias: notify_no_raining_for_2days
    trigger:
    • platform: state
      entity_id: binary_sensor.raining_weather
      to: ‘off’
      for: “48:00:00”
      action:
    • service: notify.mobile_app_newipone
      data:
      message: No rain for 2 days!
      title: ‘The garden needs water}’

I will define it in my irrigation automation if i get an notification.