I would like to calculate the doubling rate of the COVID-19 data.
The formula is: ln(2) / ln(1 - (n_today-n_yesterday) / n_yesterday) ln means the mathematical function natural logarithm.
Questions:
Is there a way to use the yesterdays value of a sensor?
How to calculate a natural logarithm?
There’s no good way to do that with home assistant yaml or templates. I would just write a script to calculate it. AppDaemon would work fine…or even a bash script.
In fact…
< several hours later >
Ok, this took way longer than I thought. Learned a bunch though.
Put this script somewhere HomeAssistant can reach it. /usr/bin/ might work? “/usr/bin/covid_calculate.sh”
#!/bin/bash
# Change these 3 things.
ha_local_ip=192.168.0.23:8123
covid_sensor=sensor.covid_sensor
api_token=<insert long lived token here>
# Get the current value of the state.
n_today=$(curl -s -X GET -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" $ha_local_ip/api/states/$covid_sensor | jq -r '.state')
# Get the state 1 day ago from now (not specifying a timestamp defaults to 1 day from now)
n_yesterday=$(curl -s -X GET -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" $ha_local_ip/api/history/period?filter_entity_id=$covid_sensor | jq -r '.[0][0].state')
# Use bc to calculate the value.
echo "l(2) / ( l(1 - (($n_today-$n_yesterday)/$n_yesterday)))" | bc -l
With the RESTful API, we can actually get history information (didn’t realize this). So all I do is read the current value of the sensor, then read the 1 day previous sensor value. By getting [0][0].state, this is the 1 day ago value as of right now.
Now just create a command_line_sensor and use this command.
sensor:
- platform: command_line
name: covid_double_rate
command: "/usr/bin/covid_calculate.sh"
# Only update once per day
scan_interval: 86400
Had to do it this way because command_line_sensor cannot take a template for the command. So all values had to be hard coded in the script (i.e. we can’t pass in the sensor.covid_sensor values. And even if we could, there’s no simple way to get state history inside HA.
I had a lot of trouble with getting the “yesterday” value too. I came up with two solutions. The only pure HA solution I came up with was using input_number.today_deaths and input_number.yesterday_deaths and then creating an automation that ran whenever my other covid trackers update that would put the input_number.today_deaths in the input_number.yesterday_deaths and then put the new number in input_number.today_deaths.
However, this got complicated as I tried to track multiple places and had multiple updates. The solution I ended up with was actually using InfluxDB. Those types of queries are super specific to how your DB is setup, you can read more detail about mine in How a computational biologist tracks COVID-19 pt 1 of?
Thanks for you ideas.
But: They look a bit to complicated for ME.
Meanwhile I made an excel sheet, which I have to update every day manually with the totals of the last day.