snarky-snark / home-assistant-variables

A custom Home Assistant component for declaring and setting generic variable entities dynamically.
Apache License 2.0
275 stars 16 forks source link

Can't get `value_template` to work #63

Closed Timmiej93 closed 2 years ago

Timmiej93 commented 2 years ago

I'm not sure if it's a bug, or if I'm just not understanding it correctly. This is how I have the variable setup in the yaml file:

update_diun:
  friendly_name: "Diun"
  initial_value: "off"
  icon: mdi:update
  value_template: "{{ states('var.update_diun') == 'on' and 'Update available' or 'Clear' }}"

If I plug the template into HA dev tools, it shows "Update available" or "Clear" perfectly. However, the variable only shows on or off in the HA GUI. Am I doing something wrong here?

danielbrunt57 commented 2 years ago

I cannot get it to work either but in my case I end up with the state as the template string. I've tried it this way

  saved_destination:
    friendly_name: Saved Destination
    value_template: "{{ states('input_select.destination') }}"

and this

  saved_destination:
    friendly_name: Saved Destination
    value_template: >-
     {{ states('input_select.destination') }}

image

snarky-snark commented 2 years ago

Neither of you is specifying a trigger to update the variables.

Add

  tracked_entity_id: input_select.destination

to your var config

See https://github.com/snarky-snark/home-assistant-variables#automatic-updates

danielbrunt57 commented 2 years ago

Thanks but I do not want it automatically updated. I want to use it to save the state from input_select.destination before HA shuts down so I can restore the state after startup as HA resets my input_select.destination back to Home when it starts up.

I've removed initial_state, value_template and tracked_entity_id and am just using it as a manually updated var entity.

Timmiej93 commented 2 years ago

Neither of you is specifying a trigger to update the variables.

Add

  tracked_entity_id: input_select.destination

to your var config

See https://github.com/snarky-snark/home-assistant-variables#automatic-updates

Right, that makes sense. I didn't read that section thoroughly since I expected value_template to be assessed on state change, and expected the "Automatic updates" section to be about updating based on other entities.

I can confirm that it does work when using the tracked_entity_id. The config does need a slight adaptation though:

update_diun:
  friendly_name: "Diun"
  initial_value: "off"
  icon: mdi:update
  value_template: "{{ (states('var.update_diun') == 'on' or states('var.update_diun') == 'Update available') and 'Update available' or 'Clear' }}"

Without this change, the variable will always show "Clear", because as soon as it's set to "Update available" it re-evaluates, and "Update available" is not equal to "on", so it resets to "Clear".