Create sensor data from another sensor with bounds OR display data in another form

Hello everyone,

I have some sensor data (numerical) that i display in a vertical-stack chart, that is working great!
Beside that, I want to display the same data also on a vertical-stack chart, but simplified. (and to be compareable to another set of data, outside home assistant)

Therefore de chart should only show numbers from 1-6, depending in which boundaries the real value is. For example the real value is between …

  • 0-100 → output value 1
  • 100-250 → output value 2
  • 250-550 → output value 3
  • 550-700 → output value 4
  • 700-9999 → output value 5

how should I go about this? Is it possible to create additional sensor data that is based on the real sensor?

Set up a template sensor (under Helpers) with this for the state template, swapping in your source sensor entity ID:

{% set m = {100:1,250:2,550:3,700:4,9999:5} %}
{{ m[m.keys()|select('>=',states('sensor.YOUR_SENSOR')|int(0))|first] }}

Or if you don’t mind a bit of template black magic:

{{ {100:1,250:2,550:3,700:4,9999:5}|dictsort|selectattr(0,'>=',states('sensor.YOUR_SENSOR')|int(0))|first|last }}
1 Like

Thank you for your magic. Could it be, that is will be saved as string instead of an integer? Because I am not able to display the data in a line chart. (I used the first template.)

Bildschirmfoto 2024-02-06 um 21.04.02

//edit:
the issue of not beeing able to show the data in a line chart was because I had no unit defined for the entities.

In addition to this, is there a good documentation of how home assistant is using this coding blocks? I know it is some form of phyton, but I have never seen this piping like behavour which i only know of linux shells like bash.
I could maybe also make use of this. I do not know phyton very well, but I am familiar with other high level languages.

Go to Developer Tools / Template, and follow the two links there. HA uses Jinja with some “extensions”; and a lot of Python methods work too.

1 Like