Appdaemon HA Sensor State

Hi
I’m trying to rewrite an HA automation to AppDaemon. I’m using Hue Dimmer switch for different functions in Home Assistant. The dimmer button value and update time is a sensor in HA and the automation calls a Python script, when both change.

- id: hue_kok_dimmer_automation
  alias: Hue Kok dimmer automation
  trigger:
    - entity_id: sensor.kok_dimmer_lastupdated
      platform: state
    - entity_id: sensor.kok_dimmer_button_status
      platform: state
  action:
    - service: python_script.hue_kok_dimmer

Name: sensor.kok_dimmer_button_status
Value: 1002 (it can be 1000, 1002, 2001, 2003 etc, always 4 integer )
Name: sensor.kok_dimmer_lastupdated
Value: 2018-07-18T17:29:28

I followed the totorial, but I can’t get it to work. The error i get is “TypeError: listen_state() missing 1 required positional argument: ‘entity’”. I tried without new=“1002” as i read in manual it should be optional, but the I get no response at all. I would prefer without the new=“1002” as just check witch button is pushed in the btn_action function.
The hello function works in AppDaemon, so it should be working properly.

Thanks
/Lars

import appdaemon.appapi as appapi
import datetime
from include import hue3 as hue

class HueKokkenButton(appapi.AppDaemon):
    def initialize(self):
        self.listen_state(self.btn_action,"sensor.kok_dimmer_button_status", new="1002")

    def btn_action(self, entity, attribute, old, new, kwargs):
        self.log("Kok Btn")
        self.turn_on("switch.sonoff02")

Your import statement is wrong - it should be:

import appdaemon.plugins.hass.hassapi as hass

The import you are using comes from older docs. The most up to date docs are here:

http://appdaemon.readthedocs.io/en/latest/

Ah, missed that. Thanks
I can only get it to work if I include new=“1002”. I tried leaving it blank or using * etc. The 4 buttons on the dimmer got 3-4 states, so I have to have many similar lines.
self.listen_state(self.btn_action,"sensor.kok_dimmer_button_status", new="1002")

Is there a better/smarter way to do this?
Regards
/Lars

the new parameter is really just a filter, without it you should see every state change for the entity, and you can check it’s new value in the callback to decide what to do.You just need to leave the whole “new=” parameter out of the listen_state call.