1mfaasj
(Bowa)
May 5, 2023, 6:03am
1
Hi, is it possible to get a percentage based an amount that changes on the last hour or day? For example I have a sensor that collects an amount, if this drops 5 percent in a certain hour I want to see this percentage in HA. such as: -5% in the last hour. And the same for the latest day.
thanks
tom_l
May 5, 2023, 6:24am
2
It depends on what percentage you want. Percentage change from the current value is easy:
Calculate the numerical change from 1 hour ago and 24 hours ago.
sensor:
- platform: statistics
name: "Hourly Change"
entity_id: sensor.your_sensor_here
state_characteristic: change
max_age:
hours: 1
- platform: statistics
name: "Daily Change"
entity_id: sensor.your_sensor_here
state_characteristic: change
max_age:
hours: 24
Convert to a percentage of the current value on the hour and at the end of the day:
template:
- trigger:
- platform: time_pattern
hours: "/1" # trigger every hour
sensor:
- name: "Percent Hourly Change"
unit_of_measurement: "%"
state: "{{ 100 * states('sensor.hourly_change')|float(0)/states('sensor.your_sensor here')|float(1) }}"
availability: "{{ states('sensor.hourly_change')|is_number and states('sensor.your_sensor here')|is_number }}"
- trigger:
- platform: time
at: "0:00:00" # trigger at midnight
sensor:
- name: "Percent Daily Change"
unit_of_measurement: "%"
state: "{{ 100 * states('sensor.daily_change')|float(0)/states('sensor.your_sensor here')|float(1) }}"
availability: "{{ states('sensor.daily_change')|is_number and states('sensor.your_sensor here')|is_number }}"
Or if you want rolling hourly and 24 hourly % change just remove the triggers.
template:
- sensor:
- name: "Percent Hourly Change"
unit_of_measurement: "%"
state: "{{ 100 * states('sensor.hourly_change')|float(0)/states('sensor.your_sensor here')|float(1) }}"
availability: "{{ states('sensor.hourly_change')|is_number and states('sensor.your_sensor here')|is_number }}"
- sensor:
- name: "Percent Daily Change"
unit_of_measurement: "%"
state: "{{ 100 * states('sensor.daily_change')|float(0)/states('sensor.your_sensor here')|float(1) }}"
availability: "{{ states('sensor.daily_change')|is_number and states('sensor.your_sensor here')|is_number }}"
If you want % cahnge compared to the value an hour ago or 24 hours ago that gets a little trickier. You have to store those values and use them later.
1 Like