How to make such automations

Hi all, I want to something like ‘home assistant’ with such features:
-when door sensor changes check if motion sensor detected movement for last minute - if so then: turn off light, check weather and say it, check my upcoming events from google calendar and say them
-otherwise, turn on light and say how long I was away
-other voice notifications for movement events

So I expect these conditions to be pretty complex so I’m going to create my own integration. I started learning python so I think I see this task as to create some ‘box’ with input data (time events and events from movement and door sensor) and output data (another events on which I can subscribe in automation and do actions).

I don’t quite understand where to start. Which class I need to subclass to be able to read sensor events? How to emit my own events? Would be grateful for code examples

Sounds like a relatively simple automation to be honest, with a choose.

3 Likes

I don’t thunk it makes sense to create a custom component for this.
This should all be doable with native HA automations.

2 Likes

Yep, I agree with both Tink (though other syntax(es) are available :rofl: ) and Burning.
Fairly trivial with minimal loading on the system, no issues with upgrades leaving your integration requiring major re-writes to keep up etc.

Do you have the names of the relevant entity_id’s ?

Well I don’t think it’s easy to make long list of triggers, conditions and actions. I’m a java developer and I’m gonna get some python experince so it’s the best solution

@Mutt, what integrations can be used to do this?

I have 2 motion sensors (gonne buy 2 more):
binary_sensor.xiaomi_motion_1 (and _2)
and 2 door sensors (binary_sensor.xiaomi_door_1)

Very simplisticly:

trigger:
- trigger: state
  entity_id: binary_sensor.front_door
  to: 'on'
action:
- choose:
  - conditions:
    - condition: state
      entity_id: binary_sensor.motion
      state: 'off'
      for: '00:02:00'
    sequence:
    - service: light.turn_on
      entity_id: light.hall
    - service: tts.say
      data:
        entity_id: media_player.hall
        message: "You've been away forever"
  default:
  - service: light.turn_on
    entity_id: light.hall
  - service: tts.say
    data:
      entity_id: media_player.hall
      message: "The weather is {{ states('weather.home') }} and your next appointment is {{ state_attr('calendar.leo','message') at state_attr('calendar.leo','start_time') }}"

(I just threw that together from the docs, it may need a bit of tweaking)

2 Likes

It’s easier without using Python. With python you need to do all the error handling and lots of other stuff yourself.

If you really want to use python for this, then use AppDaemon or PyScript.

I’ve been (and still am partially) using AppDaemon for quite some time, but I’m now slowly moving everything over to native Home Assistant automations (@Mutt see, the day has finally come haha) because it’s just not worth the effort for me to keep it all up-to-date and for lots of automations using Python makes it more conplicated then it needs to be.

1 Like

No, it’s really not.

If you follow the example from @Tinkerer there are no ‘integrations’ required, they are all native calls.

@Burningstone, Good for you, even if it’s just an exercise to prove you can.
I would be interested in how your RAM CPU Page File etc. compares pre and post (though I accept that unless you can do the conversion in about 4 hours (I’d probably take about 4 weeks :rofl: ) then it won’t be compared against the same functionality (stuff moves continuously) It will still be a worthwile test case. AND … What an achievement. Go for it :+1:

2 Likes