Hi, I have created this sensor ‘soc predicted charge’, and need support for it to default, to 45 if the source sensor becomes unavailable. I have tried with availability but cannot get the code to work.
First line defines the input:output mapping; second line pulls the output from your other sensor, set to default to a “flag” value of -99 if it is not available.
This assumes that your other sensor doesn’t go below 0 — it will return 45 for any negative value from -0 to -99, and break for any value below -99. Adjust the mapping to suit if that assumption is wrong.
An alternative would have been a simple addition to the start of your existing template:
It’s just a Jinja2 / Python dictionary, a set of key:value pairs. You’ll usually encounter it with string keys and possibly more complex structures in the values — I’ve used it with numbers on both sides here.
The inside of the square brackets returns the first key that is less than or equal to the other sensor; the line as a whole returns the associated value.