Limit a sensor's decimal places for a calculation?

Hi everyone,

I remember reading a thread that mentioned the same idea, but I can’t find it. Except for the Gladys thread that provides the calculations: Faire une opération mathématique sur une “variable de scène” - #69 par pierre-gilles

Context
Every day, I calculate the consumption of my homelab and how much it costs me.

Every day at midnight, a scene retrieves the number of Wh consumed 24 hours ago, calculates the difference with now and multiplies it by the price per Wh from my energy provider.

  • Current consumption (Wh) - Yesterday’s consumption (Wh) = Consumed in 24h
  • Price per kWh * consumed in 24h = Cost of my lab over 24h

Here’s what it looks like:

Request
The electricity price is $0.06704 per kWh (I’m in Canada). So calculating 1.52 kWh * $0.06704 = $0.1019008.

In practice, I would be interested in having only the rounded value, i.e. $0.10.

I know the library used by Gladys is Mathjs, and that there is the round() function for that. But I can’t get it to work.

@bertrandda if you pass by, I’d appreciate your expertise on this integration :slight_smile:

Hello,
you can test with the following; it should do the trick.

round(Prix_du _kWh * consommé_en_24h,2)

That’s what I had tried in a block, but maybe it simply wasn’t the right one (to send an MQTT message).

In the end, that was it. The MQTT block does not take calculations into account.
Thanks @mutmut !

image

1 Like

There is a subtlety that @prohand pointed out in his tutorial washing machine.
Il faut créer un capteur et un actionneur :

1 Like

I was a tad too slow :wink:

1 Like

Thanks for your time :slight_smile:

2 Likes

Incredible price, in any case :sweat_smile:

1 Like