snarky-snark / home-assistant-variables

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

Feature Request: Toggle Service #22

Closed jhotmann closed 3 years ago

jhotmann commented 4 years ago

There are some variables that I store as true/false (could also be stored as 1/0 or on/off) and it would be really handy if there was a service call var.toggle that would convert a variable to it's opposite to make the UI for buttons a lot cleaner.

snarky-snark commented 4 years ago

That would seem to clean up some boilerplate for those instances. I'm happy to accept a pull request. Should be easy to implement, just might take some time to test.

On the other hand, it should be pretty simple and clean to do this with the set service as it stands. Here's an untested example to give an idea.

action:
      service: var.set
      data_template:
        entity_id: var.foo
        value: {{ not (states('var.foo') | bool) }}

compared to

action:
      service: var.toggle
      data:
        entity_id: var.foo
jhotmann commented 4 years ago

I kept getting some filter error when trying to parse the boolean with | bool so I ended up getting it working like this.

    hold_action:
      action: call-service
      service: var.set
      service_data:
        entity_id: var.bedroom_motion_activation
        value_template: >-
          {% if is_state('var.bedroom_motion_activation', 'True') %} False {%
          else %} True {% endif %}