Variables with gauge

I would like to show in my Lovelace the ink level of my printer with a gauge. It’s work but when my printer is off, I can’t view the level.
So I create a variable with https://github.com/rogro82/hass-variables, and when the printer started, I update the variable level_ink_black with the value of sensor.hp_envy_4520_series_black_ink in an automation. It’s work and I can show the level in Lovelace with Entity Card and the level_ink_black variable.
But I can’t select the variable entity level_ink_black in Gauge card because it’s not a sensor.
So how can I use this variable in Gauge or have you an other solution to have the ink level when printer is off ?

Thank you

While I really like that variable integration and use it quite a bit I don’t think it is the best to use for what you want.

I would create a template sensor based on your ink level sensor:

sensor:
  - platform: template
    sensors:
      black_ink_memory:
        friendly_name: Saved Value of Black Ink
        value_template: >
          {% if states('sensor.hp_envy_4520_series_black_ink') != 'unavailable' %}
            {{ states('sensor.hp_envy_4520_series_black_ink') }}
          {% else %}
            {{ states('sensor.black_ink_memory') }}
          {% endif %}

Since you didn’t specify what the state of your sensor is when you “can’t view the level” you may have to change the test string for your sensor in the template. I assumed it was ‘unavailable’.

Then you can use that sensor in the graph.

1 Like

I’m beginner in Home assistant, so thank you for your explanation, it’s work perfectly !

I didn’t know it was possible to create sensors. I thought I had to use variable with hass-variables to save a value.
So if I understand you code, it’s create an black_ink_memory sensor. If the value of “sensor.hp_envy_4520_series_black_ink” isn’t unavailable, black_ink_memory sensor save his value. Else black_ink_memory sensor keep his value.
But I don’t understand when value_template is call ? I don’t must create an automation to update the black_ink_memory sensor ?
Also, how can I save the black_ink_memory sensor value when I restart HA ? With hass-variable, there is “restore” param. But it dosen’t work with this sensor.
And is it normal that I can’t find the black_ink_memory sensor in Configuration/Entities page ?

Thank you

The sensor is updated when ever any of the entities in the template changes.

in this case you original sensor changing causes the created sensor to update automatically.

What do you mean by that?

are you saying that if you try to add “restore: true” to the template it doesn’t work?

or are you saying the template sensor state doesn’t survive a restart?

If it’s the first one then you are correct that won’t work because it isn’t a valid option. but you shouldn’t need it.

If it’s the second I’m not sure why that would be since those sensors values should survive a restart.

Wooh it’s magic ! I didn’t know that !

I want the sensor value survive a restart.
When I shutdown my printer, gauge work with sensor.black_ink_memory. But after, when I restart my HA, the sensor.black_ink_memory as “unknown” state. It dosen’t keep his value after restart :frowning:
So I tested “restore” param like with hass-variables, but it’s not a correct option. Have you a solution please ? It’s the only problem.

Thank you

try this and see if it works:

sensor:
  - platform: template
    sensors:
      black_ink_memory:
        friendly_name: Saved Value of Black Ink
        value_template: >
          {% if states('sensor.hp_envy_4520_series_black_ink') != 'unavailable' or states('sensor.hp_envy_4520_series_black_ink') != 'unknown' %}
            {{ states('sensor.hp_envy_4520_series_black_ink') }}
          {% else %}
            {{ states('sensor.black_ink_memory') }}
          {% endif %}

It doesn’t work.
But your first solution should work because when my printer is off, the state of hp_envy_4520_series_black_ink is “unavailable” :
image

I also test with “and” instead of “or” in your condition but it’s doesn’t work.

I don’t understand the problem if normally the sensor must keep his value after HA restart :confused: