Use variables in display/draw shapes

Is it possible to use a variable to draw a shape in Esphome display component?

For example:

int volume;
volume = id(sensor.sonos_volume_int).state;
it.filled_rectangle(0,0,volume,4);

This does not work. It just displays nothing.

The idea is to draw a rectangle where the length depends on what the value is of the volume variable.
The variable declaration seems to work: I’m able to display the variable with it.printf. But is it possible to reference it in other functions?

You need to make the volume a sensor.

sensor:
  - platform: homeassistant
    id: volume
    entity_id: sensor.sonos_volume_int

That should make it usable in the display

I already have volume as a sensor. I’ve been trying this for a couple of weeks now, I think I already tried something like this.

How would I reference to that sensor in the rectangle? Something like this?:

it.filledrectangle(0,0,id(volume).state,4)

Always include all relevant parts in your question!
But yes that should be what you need. Perhaps it needs to be converted to int also

it.filledrectangle(0,0,int(id(volume).state),4)
1 Like

Sorry, sensor was not in the current config anymore (thought is was, but removed it as I was not using it anymore).

I will try this suggestion.

Yes, it works!

Lambda:

it.filled_rectangle(20, 36, id(sonos_volume_int).state, 4);

My sensor:

sensor:
  - platform: homeassistant
    id: sonos_volume_int
    entity_id: sensor.sonos_volume

The entity in the sensor already has an integer as value, so converting with int was not needed.

Thanks for the help. This opens up a whole lot of possibilities!