Thanks to liuk4friends, but I did try MQTT and found it still wasn’t persisting. Anyway, I eventually solved this by creating a series of automations (one per colour) to store the values within text files. The I created four template sensors to read the values from these text files. As the text files obviously persist over a restart, so the values are always available.
# RETAINED PRINTER INK LEVELS
- platform: template
sensors:
ink_level_black:
friendly_name: "Ink Level Black"
unit_of_measurement: '%'
value_template: >
{% if is_state('sensor.hp_photosmart_5520_series_black_ink', 'unavailable') %}
{{ states('input_text.text1') }}
{% else %}
{{ states('sensor.hp_photosmart_5520_series_black_ink') }}
{% endif %}
- platform: template
sensors:
ink_level_cyan:
friendly_name: "Ink Level Cyan"
unit_of_measurement: '%'
value_template: >
{% if is_state('sensor.hp_photosmart_5520_series_cyan_ink', 'unavailable') %}
{{ states('input_text.text2') }}
{% else %}
{{ states('sensor.hp_photosmart_5520_series_cyan_ink') }}
{% endif %}
- platform: template
sensors:
ink_level_magenta:
friendly_name: "Ink Level Magenta"
unit_of_measurement: '%'
value_template: >
{% if is_state('sensor.hp_photosmart_5520_series_magenta_ink', 'unavailable') %}
{{ states('input_text.text3') }}
{% else %}
{{ states('sensor.hp_photosmart_5520_series_magenta_ink') }}
{% endif %}
- platform: template
sensors:
ink_level_yellow:
friendly_name: "Ink Level Yellow"
unit_of_measurement: '%'
value_template: >
{% if is_state('sensor.hp_photosmart_5520_series_yellow_ink', 'unavailable') %}
{{ states('input_text.text4') }}
{% else %}
{{ states('sensor.hp_photosmart_5520_series_yellow_ink') }}
{% endif %}
This required that I also created the below input text entries in my config.yaml.
input_text:
text1:
name: Ink Level - Black
text2:
name: Ink Level - Cyan
text3:
name: Ink Level - Magenta
text4:
name: Ink Level - Yellow
And here’s the automations which tie the 2 parts together.
- id: Black Ink
alias: Record Black Ink Level
trigger:
platform: state
entity_id: sensor.hp_photosmart_5520_series_black_ink
condition:
condition: template
value_template: '{{ trigger.to_state.state not in [trigger.from_state.state, ''unavailable'']
}}
'
action:
service: input_text.set_value
entity_id: input_text.text1
data_template:
value: '{{ trigger.to_state.state }}'
- id: Cyan Ink
alias: Record Cyan Ink Level
trigger:
platform: state
entity_id: sensor.hp_photosmart_5520_series_cyan_ink
condition:
condition: template
value_template: '{{ trigger.to_state.state not in [trigger.from_state.state, ''unavailable'']
}}
'
action:
service: input_text.set_value
entity_id: input_text.text2
data_template:
value: '{{ trigger.to_state.state }}'
- id: Magenta Ink
alias: Record Magenta Ink Level
trigger:
platform: state
entity_id: sensor.hp_photosmart_5520_series_magenta_ink
condition:
condition: template
value_template: '{{ trigger.to_state.state not in [trigger.from_state.state, ''unavailable'']
}}
'
action:
service: input_text.set_value
entity_id: input_text.text3
data_template:
value: '{{ trigger.to_state.state }}'
- id: Yellow Ink
alias: Record Yellow Ink Level
trigger:
platform: state
entity_id: sensor.hp_photosmart_5520_series_yellow_ink
condition:
condition: template
value_template: '{{ trigger.to_state.state not in [trigger.from_state.state, ''unavailable'']
}}
'
action:
service: input_text.set_value
entity_id: input_text.text4
data_template:
value: '{{ trigger.to_state.state }}'
I recently replaced most of my automations with equivalents in Node Red. So here’s the flow for these too.
[{"id":"5c573fe4.d4ebb","type":"server-state-changed","z":"4427d2af.513fec","name":"Record Black Ink Level","server":"6009dbec.e68c94","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.hp_photosmart_5520_series_black_ink","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":140,"y":720,"wires":[["db080e61.fec17"],[]]},{"id":"db080e61.fec17","type":"api-call-service","z":"4427d2af.513fec","name":"Store Black Ink Level","server":"6009dbec.e68c94","version":1,"debugenabled":true,"service_domain":"input_text","service":"set_value","entityId":"input_text.text1","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":420,"y":720,"wires":[[]]},{"id":"211e314d.61686e","type":"server-state-changed","z":"4427d2af.513fec","name":"Record Cyan Ink Level","server":"6009dbec.e68c94","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.hp_photosmart_5520_series_cyan_ink","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":140,"y":780,"wires":[["f4cde81d.c28678"],[]]},{"id":"f4cde81d.c28678","type":"api-call-service","z":"4427d2af.513fec","name":"Store Cyan Ink Level","server":"6009dbec.e68c94","version":1,"debugenabled":false,"service_domain":"input_text","service":"set_value","entityId":"input_text.text2","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":400,"y":780,"wires":[[]]},{"id":"9cc7c692.8c2258","type":"server-state-changed","z":"4427d2af.513fec","name":"Record Magenta Ink Level","server":"6009dbec.e68c94","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.hp_photosmart_5520_series_magenta_ink","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":150,"y":840,"wires":[["71a49633.eb7498"],[]]},{"id":"71a49633.eb7498","type":"api-call-service","z":"4427d2af.513fec","name":"Store Magenta Ink Level","server":"6009dbec.e68c94","version":1,"debugenabled":false,"service_domain":"input_text","service":"set_value","entityId":"input_text.text3","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":410,"y":840,"wires":[[]]},{"id":"dd6fce60.3c61","type":"server-state-changed","z":"4427d2af.513fec","name":"Record Yellow Ink Level","server":"6009dbec.e68c94","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.hp_photosmart_5520_series_yellow_ink","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":140,"y":900,"wires":[["35d35a.fd84dca6"],[]]},{"id":"35d35a.fd84dca6","type":"api-call-service","z":"4427d2af.513fec","name":"Store Yellow Ink Level","server":"6009dbec.e68c94","version":1,"debugenabled":false,"service_domain":"input_text","service":"set_value","entityId":"input_text.text4","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":400,"y":900,"wires":[[]]},{"id":"6009dbec.e68c94","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
Now my printer ink levels are retained, even after a restart.
NB: This approach would obviously work for any sensor I’d assume.