Data based on other data

Hi All,

Found I can push a rest request to smartthings, to get kwh readings for my electricity. I’m just taking a sample every 30s and pushing the new value over MQTT into HA.

What would be nice, is to do some maths on that value, essentially to calculate the change in kwh from the previous reading, time since the last reading, and to then calculate current watts used.

I potentially do it in the bash script that fires off the rest request to smartthings and pushes the result to MQTT, but I’m curious if its possible to do natively in HA? Would it be a template sensor with some maths behind it? But would that be able to pull the last historical reading as well?

Or, am I better putting a small Python/Java wrapper around it and do the logic externally to HA?

One option is to use external DB (MySQL/PostgreSQL or InfluxDB) and to use SQL sensors.

you can create an automation on state change and it’ll give you from and to state objects - will it do?

Thanks both! I’m using Oracle already as an external DB, hadn’t seen SQL sensors so that looks interesting to explore.
Like the idea of automation too, with the from and to values and a little bit of maths I could publish in the computed actual Watts amount, so looks a good option as well
Many thanks