Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Fallback value of a variable to another variable #84

Open
clau-bucur opened this issue Jun 27, 2024 · 6 comments
Open

[FR] Fallback value of a variable to another variable #84

clau-bucur opened this issue Jun 27, 2024 · 6 comments

Comments

@clau-bucur
Copy link

Describe the solution you'd like
Is it possible to define the default value of a variable, as the value of another variable?

Someting like:

my_template:
  default:
    - history_entity: '[[entity]]'
  card:
    type: my_card
    entity: '[[entity]]'
    tap_action:
      action: fire-dom-event
      browser_mod:
        service: browser_mod.more_info
        data:
          entity: '[[history_entity]]'

And when I call it withoiut specifying a value for history_entity it will use the value specified for the entity variable:

- type: custom:decluttering-card
  template: my_template
  variables:
    - entity: sensor.my_entity
@ildar170975
Copy link

Agree, a good idea, was thinking about same many times.

@droans
Copy link

droans commented Jul 12, 2024

Use a conditional card.

my_template:
  defaults:
    - history_entity: 'fake'
  card:
    type: vertical-stack
    cards:
      - type: conditional
        conditions:
          - condition: state
            entity: '[[ history_entity]]'
            state: "unavailable"
        card:
          - type: my_card
            entity: '[[entity]]'
            tap_action:
              action: fire-dom-event
              browser_mod:
                service: browser_mod.more_info
                data:
                  entity: '[[entity]]'
      - type: conditional
        conditions:
          - condition: state
            entity: '[[ history_entity]]'
            state_not: "unavailable"
        card:
          - type: my_card
            entity: '[[entity]]'
            tap_action:
              action: fire-dom-event
              browser_mod:
                service: browser_mod.more_info
                data:
                  entity: '[[history_entity]]'

A non-existent sensor will always return unavailable.

@ildar170975
Copy link

ildar170975 commented Jul 12, 2024

A non-existent sensor will always return unavailable.

I would say this is a bug in HA frontend because a non-existing entity's state is unknown.

@droans
Copy link

droans commented Jul 12, 2024 via email

@clau-bucur
Copy link
Author

Use a conditional card.

my_template:
  defaults:
    - history_entity: 'fake'
  card:
    type: vertical-stack
    cards:
      - type: conditional
        conditions:
          - condition: state
            entity: '[[ history_entity]]'
            state: "unavailable"
        card:
          - type: my_card
            entity: '[[entity]]'
            tap_action:
              action: fire-dom-event
              browser_mod:
                service: browser_mod.more_info
                data:
                  entity: '[[entity]]'
      - type: conditional
        conditions:
          - condition: state
            entity: '[[ history_entity]]'
            state_not: "unavailable"
        card:
          - type: my_card
            entity: '[[entity]]'
            tap_action:
              action: fire-dom-event
              browser_mod:
                service: browser_mod.more_info
                data:
                  entity: '[[history_entity]]'

A non-existent sensor will always return unavailable.

Thanks for the suggestion, however that example is only a simple use-case where the request would be useful.

There are many more cases where the request comes handy.
Even in your example, the same end-result is much cleaner and easier achieved by using variable fallback.

@igorsantos07
Copy link

It is important to allow "templating" in the default values as well, so one could do as follows:

defaults:
  - entity: 'binary_sensor.[[area]]_presence'

This way, you could override the entity to be something else, or just go with the default based on the area name 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants