appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

Getting unreliable constrain_input_boolean at app level

Open ihaettypo opened this issue 2 years ago • 3 comments

version:

Add-on version: 0.9.2
 You are running the latest version of this add-on.
 System: Home Assistant OS 8.4  (amd64 / qemux86-64)
 Home Assistant Core: 2022.8.1
 Home Assistant Supervisor: 2022.07.0

The config:

hello_world:
  class: HelloWorld
  log_level: DEBUG
  module: hello
  constrain_input_boolean: input_boolean.hello_world
  name: HELLO WORLD

The app:

import appdaemon.plugins.hass.hassapi as hass

class HelloWorld(hass.Hass):

  def initialize(self):
    self.log("Hello world!")
    self.run_in(self.run_in_c, 1)
  
  def run_in_c(self, kwargs):
    self.log("Callback fired!")

The input_boolean yaml:

input_boolean:
  hello_world:
    name: HELLO WORLD

However, when the input_boolean is turned off, sometimes the call back went through, sometimes not.

Here's the DEBUG log:

2022-08-07 14:10:52.670635 INFO hello_world: Hello world!
2022-08-07 14:10:52.672749 DEBUG hello_world: Registering run_in in 1 seconds for hello_world
2022-08-07 14:10:54.007146 DEBUG hello_world: get state: None, {} from hello_world
2022-08-07 14:10:54.018797 INFO hello_world: Callback fired!

Why does it say get state: None, {} from hello_world, and why even so sometimes the constraint works?

ihaettypo avatar Aug 07 '22 12:08 ihaettypo

Pretty certain this is happening because of the None value being returned from get_state() which indicates, at the time your code is running, AD doesn't have the input_boolean.hello_world entity stored in memory. If you want more flexibility with constraints, you can write a custom constraint into your code or add if/else statement within your callback.

Justihar avatar Aug 08 '22 02:08 Justihar

Pretty certain this is happening because of the None value being returned from get_state() which indicates, at the time your code is running, AD doesn't have the input_boolean.hello_world entity stored in memory. If you want more flexibility with constraints, you can write a custom constraint into your code or add if/else statement within your callback.

Thanks for replying. I'm using it exactly the way mentioned in the documentaion here, if I understand it correcly. So maybe this is a bug?

ihaettypo avatar Aug 08 '22 02:08 ihaettypo

Yeah I'm not suggesting your usage of the constraint is incorrect and it's also probably not a bug, depending on how you look at it. I believe the constraint implicitly runs if get_state() returns None, which is where the real problem is. Just out of curiosity, try changing the run_in() within initialize() to self.run_every(self.run_in_c, self.get_now(), 5) with the input_boolean turned on. Let it settle for a few cycles then try toggling it on/off to test. You can adjust the interval as needed. I suspect that after a few seconds, the input_boolean constraint will work right every single time.

Justihar avatar Aug 08 '22 03:08 Justihar