Sensor that shows change in temperature

Is it possible make a sensor that takes the current value of another sensor and compares it with the state of the same sensor an hour back in time?

I want to see if the temp in my house has gotten colder or hotter compared to an hour ago and also for the sensor to show the difference in +C och -C. Even better would be in green color or blue color depending on positive or negative.

I have tried using the binary_sensor but I’m not getting any good results and I dont even know if it would do the job as I want it to show in the end.

binary_sensor:

  • platform: trend

If you just want to visualize it, take a look at the new sensor card or if you are on the old UI the history graph component works.

https://www.home-assistant.io/components/history_graph/

I do this exact thing in my configuration.

Are you using influxdb? If so, you can use the influxdb sensor to fetch historic values and perform mathematical functions for you.

Since I’m using a pretty small tablet to view everything in the house I’m just interested in a small single value sensor that doesnt take much space. I’m using influx/grafana and have a couple of graphs through a panel_iframe there but in this particular case I just want a number that fits my already overwhelmed dashboard :slight_smile:

since we already can subtract a sensor value from another sensor, all we need in this case is to fetch or store a value every hour to be used for subtraction. I guess this can be done with an automation of some sort. I just wondered if there was some kind of complete solution or component that I have missed. It would be a very nice feature to have in a monitoring system, seems pretty standard to me also :slight_smile:

What if I make an input_number sensor, how can I then use it in an automation to store a value from an existing sensor in it?

If I can achieve this then I think the case is solved with a template sensor:
“current-sensor” minus “input_number sensor” that gets its value stored with the automation every hour.

Does this help? https://www.home-assistant.io/components/binary_sensor.trend/

There are a number of utility sensors you might like to look through.

Why not simply use the statistics sensor? https://www.home-assistant.io/components/sensor.statistics/

This would give you the change over the defined period (and some other statistics) as attribute which you can then use to create a template sensor. Something like this:

sensor:
  - platform: statistics
    name: temp_stats_1h
    entity_id: sensor.your_temperature_sensor
    max_age:
      minutes: 60

  - platform: template
    sensors:
      temp_change_1h:
        value_template: "{{ state_attr('sensor.temp_stats_1h_mean','change') }}"
        unit_of_measurement: '°C'
2 Likes

wow, this seems to be exactly what I’m looking for. I’ll give it a go tonight!

Nickrout: I’ve tried that one, firstly I’m doing something wrong and cant get it working correctly… but secondly I dont think it returns a calculated value back like I want. It only gives a trend “up” och “down” value I think?

I would use influxdb sensor to retrieve the value from 1 hour ago and then a template sensor to find the difference between current value and old value.

Why influxdb? There is a component to access the home assistant database.

Anyway looks like @Mattie has it sorted :slight_smile: I find all those utility sensors quite confusing and generally have to experiment to get them right.

So easy when you know what to do, this worked like a charm! Thank you very much sir! :slight_smile:

Hey Mattie,

I stumbled over this post and I’d be really curious what that code looked like on your end.
I got a similar idea, but not with temperatures.
I’d like to record the daily change of the corona virus infections compared to the day before.
My code currently looks like this:

- platform: statistics
  name: current_stats_1h
  entity_id: sensor.worldwide_coronavirus_confirmed
  max_age:
     minutes: 10

- platform: template
  sensors:
    sensornametest:
      value_template: "{{ state_attr('sensor.current_stats_1h_mean','change') | int}}"
      unit_of_measurement: "people"

But somehow it doesn’t give me anything. :frowning:

Hey MrBenedict,

my code looks like below, hope it helps :slight_smile:

  - platform: statistics
    name: utomhus_stats
    entity_id: sensor.utomhus_temperature
    max_age:
      minutes: 60

  - platform: template
    sensors:
      utomhus_trend:
        friendly_name: Utomhus
        value_template: "{{ state_attr('sensor.utomhus_stats','change') }}"
        unit_of_measurement: '°C'
        icon_template: >-
          {%- if state_attr('sensor.utomhus_stats','change') | float < 0.01 %}
            mdi:arrow-down-bold
          {% else %}
            mdi:arrow-up-bold
          {% endif %}
1 Like

I’m trying to set this up at my end, but I’m having some issues:

My code looks like this:

    name: livingroom temperature stats
    entity_id: sensor.atc_livingroom_temperature
    max_age:
      minutes: 60

  - platform: template
    sensors:
      outdoor_temp_trend:
        friendly_name: Livingroom temperature trend
        value_template: "{{ state_attr('livingroom_temperature_stats','change') }}"
        unit_of_measurement: '°C'
        icon_template: >-
          {%- if state_attr('livingroom_temperature_stats','change') | float < 0.01 %}
            mdi:arrow-down-bold
          {% else %}
            mdi:arrow-up-bold
          {% endif %}

Trend returns unknown

If I try to check the template {{ state_attr('livingroom_temperature_stats','change') }} in developer tools, it returns null.

Any idea why is this happening?

Actually my two sensors also happen to return unknown since a few weeks ago, I havent had the time to investigate it but now I’m almost wondering if it has something to do with the later versions…? I’ll write here if I find the time to check

1 Like

Fixed it.
There is a breaking change in 2021.12.

New code looks like this:

  - platform: statistics
    name: livingroom temperature stats
    entity_id: sensor.atc_livingroom_temperature
    state_characteristic: change
    max_age:
      minutes: 60

  - platform: template
    sensors:
      indoor_temp_trend:
        friendly_name: Livingroom temperature trend
        value_template: "{{ states('sensor.livingroom_temperature_stats') }}"
        unit_of_measurement: '°C'
        icon_template: >-
          {%- if states('sensor.livingroom_temperature_stats') | float < 0.01 %}
            mdi:arrow-down-bold
          {% else %}
            mdi:arrow-up-bold
          {% endif %}
2 Likes

Nice, I guess I’m the one who will be thanking you :slight_smile:

1 Like

state_characteristic: change

wast this the only difference? I’m still not getting mine to work

nevermind, missed the attribute change