snarky-snark / home-assistant-variables

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

global versus local... #104

Closed pvklink closed 5 months ago

pvklink commented 9 months ago

Can variables made global, even if they are declared in a choose scenario? In the below (part of my) script i update the var e1 two times, At the start and when a condition matches... My problem is that i want to overule the first declaration within a choose construct, at the end of the script the first declaration is available...

What am i doing wrong"

I use he latest HASS beta on a docker on rpi 4b with the latest OS.

sequence:

danielbrunt57 commented 9 months ago

1. A particular variable only exists once (i.e. globally). You can't have two different instances of the same named variable & different values.

2. Review your automation/script traces to make sure your code is doing what you want.

2. Format your code with the code wrapper: <>. It should then look something like this:

My code:

service: var.set
data:
  entity_id: var.batteries_replaced
  attributes:
    "{{ trigger.entity_id }}": "{{as_timestamp(now()) | timestamp_custom('%Y-%m-%d') }}"

I don't know if your code is correct or not. This is my guess at what your code's choose: might look like...

- choose:
    - conditions:
        - condition: or
          conditions:
            - condition: template
              value_template: "{{ debug == 'scenes_unlock' }}"
            - condition: and
              conditions:
                - condition: template
                  value_template: "{{ debug == 'scenes_single' }}"
                - condition: template
                  value_template: "{{ actiontrigger == 'input_boolean.scene_opstaan' }}"
      sequence:
        service: var.set
        data:
          entity_id: var.e1
          value: 2
pvklink commented 9 months ago

Hi,

Yes, the sequence you reproduced is same as mine… Problem is that the declaration of a variable is not global (one value in all situations) when you give it values in or out a choose condition….

Verzonden vanuit Mail voor Windows

Van: Daniel Verzonden: zaterdag 9 december 2023 02:45 Aan: snarky-snark/home-assistant-variables CC: peter van klink; Author Onderwerp: Re: [snarky-snark/home-assistant-variables] global versus local...(Issue #104)

1. A particular variable only exists once (i.e. globally).

You can't have two different instances of the same named variable & different values.

2. Review your automation/script traces to make sure your code is doing what you want.

2. Format your code with the code wrapper: <>. It should then look something like this:

My code: service: var.set data: entity_id: var.batteries_replaced attributes: "{{ trigger.entity_id }}": "{{as_timestamp(now()) | timestamp_custom('%Y-%m-%d') }}" I don't know if the code is correct. This is my guess at what your code's choose: might look like...

pvklink commented 9 months ago

I solved the issue with another variables integration… https://github.com/enkama/hass-variables With this integration i can set variables regardless where i change them in or out a choose construction

Verzonden vanuit Mail voor Windows

Van: Daniel Verzonden: zaterdag 9 december 2023 02:45 Aan: snarky-snark/home-assistant-variables CC: peter van klink; Author Onderwerp: Re: [snarky-snark/home-assistant-variables] global versus local...(Issue #104)

1. A particular variable only exists once (i.e. globally).

You can't have two different instances of the same named variable & different values.

2. Review your automation/script traces to make sure your code is doing what you want.

2. Format your code with the code wrapper: <>. It should then look something like this:

My code: service: var.set data: entity_id: var.batteries_replaced attributes: "{{ trigger.entity_id }}": "{{as_timestamp(now()) | timestamp_custom('%Y-%m-%d') }}" I don't know if the code is correct. This is my guess at what your code's choose: might look like...

danielbrunt57 commented 9 months ago

Yes, the sequence you reproduced is same as mine… Problem is that the declaration of a variable is not global (one value in all situations) when you give it values in or out a choose condition….

That's bizarre!

danielbrunt57 commented 9 months ago

I solved the issue with another variables integration… https://github.com/enkama/hass-variables With this integration i can set variables regardless where i change them in or out a choose construction

I also have just switched another variable of mine to that integration as I was trying to save & restore attributes in a variable and it just plain was not working no matter what I tried. I either couldn't alter the attribute or when I could, they would vanish on a restart.