Getting a sensor to report degrees per minute temp change

I am floundering around with little I can find in the way of examples.

I want to close curtains when the sun starts shining into a room and heating it up. I have tried elevation and azimuth but due to buildings obstructing the sun as seasons change I want to drive the automation by the temperature sudden rise.

I have created a sensor using a statistics template and have a sensor extract the change_rate from such a sensor but it gives no useful values, either 0 or 0.0, so I must be missing a crucial step.

# make HA log stats for the bedroom temp
- platform: statistics
  name: bedroom_temp_stats
  entity_id: sensor.temperature_12

# extract the temp change rate
- platform: template
      friendly_name: Bedroom Temp Rate
      value_template: "{{ state_attr('sensor.bedroom_temp_stats', 'change_rate') }}"
# extract the temp change
      friendly_name: Bedroom Temp Change
      value_template: "{{ state_attr('sensor.bedroom_temp_stats', 'change') }}"

The additional sensor I added ‘change’ gives me numbers but I’ve no idea what the value means, a change from what? since when ?

Help !

I know its Christmas but …

Ok, so lacking any feedback I am working on the assumption that the change_rate attribute in the Statistics is not functioning at present so I have created a solution which meets my need.

It is an SQL query and would probably work on any sensor.
It takes the two most recent state records for the chosen sensor, merges them into single record and then calculates the change between the two sensor states and divides this difference by the number of minutes between the two events. This gives a change per minute and is then recorded in a sensor so creating a history of changes which can then be graphed if you want.

I currently use this to trigger the closing of curtains if the temp rises by more than 0.05 degrees in a minute which is what happens when the sun (currently very smokey) strikes the window frame my temperature sensor is stuck to.

- platform: sql
    - name: bedroom_dpm
      query: "Select st1-st2 as chg, julianday(t1)*24*60-julianday(t2)*24*60 as tm,
(st1-st2) / (julianday(t1)*24*60-julianday(t2)*24*60) as degpermin
From (
Select max(state) st1, max(last_updated)t1,max(s2state)st2,max(s2updated) t2
From (
SELECT s1.state, s1.last_updated , null as s2state, null as s2updated
FROM states s1
Where domain = 'sensor' and entity_id = 'sensor.temperature_12' and state <> 'unknown'
And last_updated = (select max(last_updated) from states where entity_id = 'sensor.temperature_12' and state <> 'unknown')
SELECT null, null, s2.state, s2.last_updated 
FROM states s2
Where domain = 'sensor' and  entity_id = 'sensor.temperature_12' and state <> 'unknown'
And last_updated < (select max(last_updated) from states where  entity_id = 'sensor.temperature_12' and state <> 'unknown')

Order by last_updated desc, s2updated desc
Limit 2
) p1
) p2 "
      column: 'degpermin'
      unit_of_measurement: "°C/min"
1 Like