Sensor creation

I’m just trying out AppDaemon (2.1), and it’s not clear to me how best to create a HomeAssistant sensor whose value can be set in AppDaemon.

For example, I have a sensor named ‘house_occupancy’ that defines and maintains the occupancy state for the house (i.e. occupied, unoccupied or unknown). I would like to control the state of house_occupancy with AppDaemon.

Should I create house_occupancy in my HomeAssistant configuration file, with an empty value template, or can I / should I create it within AppDaemon?

I had the same dilemma. I didn’t try an empty value template sensor because I figured HA might step on it. I didn’t actually try.

What I did was create a custom_component and started with a copy of the template sensor. I stripped out the processing of the value template inside async_update (I left in icon and entity picture, but not using those).

Seems to be working. Try out a template sensor and see. Maybe we don’t need to jump thru these hoops if an out of the box template sensor will work.

if you want to create a new sensor, that you only want to control from appdeamon, then you dont have to do anything in HA.

as soon as you use set_state for the first time, the sensor will automaticly be created.
please dont try to create an empty sensor in homeassistant (and for sure not a template) because it will only give problems.
a sensor in HA will automaticly be updated every time, based on the settings given in HA.
no or blind settings will then reset the sensor every time, making it hard to control it from AD.

so @rhodges you dont need to jump thru hoops at all, just dont do anything in HA. :wink:

1 Like

Ok, many thanks. Just to clarify, if you create a sensor in AD, will it also exist in HA?

@ReneTode Thanks. I don’t remember why I jumped those hoops. Now that you mention it, I think I only did that to try and solve an unrelated issue.

Oh, I think I know why I did that. I wanted to customize the sensor. In AppDaemon I only wanted to set the state and nothing else. I would attempt to read the sensor attributes, then pass those and the new state to set_state. I was having issues because attributes would be empty when I read them and thus not have any of the customized stuff.

The gutted template sensor got around the issue. Since there was a “real” sensor, when I read the state and got the attributes, I would actually get my customizations and thus the set_state call in AppDaemon worked as expected.

the sensor will exist in HA as soon as AD does a set_state for the first time.
it will dissapear again as soon as you restart HA and reappear as soon as AD does a set_state again.
I do set the state from my AD sensors in 1 particular app. so that when i restart HA (and then start AD) i will have all those sensors ASAP.

@rhodges you can also set all attributes with set_state like this:

        self.set_state("sensor.the_name", state = "anything_you_like", attributes = {"friendly_name": "What you want in the frontend"]})
5 Likes

@ReneTode Yeah, I was just trying to avoid that. I was able to keep things a lot more DRY inside AppDaemon by having the sensor created with the attributes already specified. I had 10+ sensors, each splitting into 3 other sensors, each with a custom name, icon and uom.

If this is not a requirement for the OP then I would agree, keep it simple and do it all in the AppDaemon set_state.

i would like to create all my entities in AD.
i can manipulate them easier, without the need to restart when i want to add or modify something.

Many thanks for all the advice. A follow up question: If you create a sensor (or switch) in AppDaemon, presumably you can monitor its state in the same way as a sensor created in Home Assistant?

I’ve made a switch in an AppDaemon app initialize function, which aims to enable or disable motion-detection lighting in a particular room. I want to use a switch widget in a dashboard to enable or disable motion-sensitive lighting.

import appdaemon.appapi as appapi

class MotionLights(appapi.AppDaemon):

    def initialize(self):
        self.enabled = True
        self.room = self.args.get("room")
        switch_name = "switch.{}_motion_lighting".format(self.room)
        frName = "{} motion lighting".format(self.room).title()
        self.set_state(switch_name,state='on',attributes={"friendly_name":frName})
        self.listen_state(self.motion_switch_change, switch_name)

    def motion_switch_change(self, entity, attribute, old, new, kwargs):
        self.log('Motion switch state change: {}'.format(old,new))
        self.enabled = False # or True, depending on values of old and new...

However, the app does not seem to receive any state changes, and the motion_switch_change is never called… Should this work or am I doing something wrong?

i am sorry to say that creating sensors is goin like a charm, but input_booleans and switches seem not to work like it should.
it should be possible to create them, but i never got a decent answer why its not working.

so i create input_booleans in HA when i want to switch something in AD.

Yes, seems to be the case. I’m getting some fairly strange behaviour from the switches (and now input booleans) that I’ve created.

I’ve created several sensors in Appdaemon using “set_state”, this works just fine but do I just have to live these warnings in my Appdaemon log?

WARNING hue_button_handler: hue_button_handler: Entity sensor.button_hue1 not found in namespace default