Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 101645

Sensor.command_line, templates and secrets - how to survive HA restart?

$
0
0

@AhmadK wrote:

I need to save some text info between HA shutdown and restart so I can restore it.
I thought I can save it to local file and apparently the only standard-ish way to do so is to use notify.file to save and sensor.file to load that data.

I don’t want to hard-code filename so I put it in secrets.yaml and here is my notify sensor:

- name: save_central_heating_settings
  platform: file
  filename: !secret central_heating_saved_settings_filename

It always stores the file in HA config folder so there is no need in full path, just a filename.

However, the sensor.file requires a full path (and even more, it won’t work unless you whitelist HA config folder - have no idea if it’s a bug or they overcomplicated things because the same goal can be achieved by using sensor.command_line without any whitelisting) so I need to create another !secret to make it work - a bit ugly:

- platform: file
  name: central_heating_saved_settings
  file_path: !secret central_heating_settings_file_path

and in secrets.yaml:

central_heating_saved_settings_filename: 'central_heating_settings.txt'
central_heating_saved_settings_file_path: '/config/central_heating_settings.txt'

I thought I can live with only one !secret by using sensor.command_line as unlike sensor.file it support templates in command BUT: you cannot use a value of !secret variable inside templates so it should make its way into a template sensor.
Ok, here is the template sensor

- platform: template
  sensors:
    central_heating_saved_settings_filename:
      value_template: !secret central_heating_saved_settings_filename

and the sensor.command_line

- platform: command_line
  name: central_heating_saved_settings
  command: "tail -n 1 /config/{{ states('sensor.central_heating_saved_settings_filename') }}"

Again, no funny business is allowed in !secret used as a template, it should be the complete template.
The resulting thing works BUT it gives me the following error message in HA log on every restart:

ERROR (SyncWorker_6) [homeassistant.components.command_line.sensor] Command failed: tail -n 1 /config/unknown

and I completely understand why - the sensor.central_heating_saved_settings_filename doesn’t exist or is not ready yet (but it will be very soon forcing sensor.central_heating_saved_settings to re-evaluate the template and to read the proper file).

Ideally to avoid that error I’d create that command line sensor on HA startup dynamically when sensor.central_heating_saved_settings_filename is ready, but I don’t know how to do that (and actually I doubt it’s allowed if I’m not using a custom_component).

Does that mean back to square one (using 2 !secret variables) or there is another way?
I know it sounds very overcomplicated, but in fact I stumble upon such limitations from time to time and they are very annoying, too…

P.S I’ve just thought about using retained mqtt messages for that, but haven’t tested it yet… the thing is it calls for MQTT being up and running (which is not always the case) while local filesystem is always there.

Posts: 5

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 101645

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>