Catching Tradfri motion sensor trigger

tl;dr: I want to catch the trigger-events from the Tradfri motion sensor and only pass some of them to turn on the lights.
So far i failed to catch the events and need a good idea there…

Long version:
I have a bigger amount of Tradfri devices, which all works fine so far.
The only thing which i can’t get to work as i want to is the motion sensor. Its not triggering the lights the way i want.

  1. the shortest time to put the lights on it 1 minute. I want this to be variable, depending on time of day [optional]
  2. when i put on the lights my remote and the motion sensor triggers, the lights turn off after 1 minute. This is what annoys me the most, cause i want the lights to turn off automaticly only if they were triggered on by the motion sensor. So putting lights on manually should overwrite the motion sensor settings until i put them off manually again.
  3. motion sensor should only work, when sun is set but before midnight, as my cats are triggering the lights all the night long ^^

My first try was installing a third party system (as HA) first, which should manage the lights and sensors. This does not work, because the motion sensor communicates with the lights directly and you cannot manage the events as a “man in the middle” system.

So my seconds try was to unpair the lights and the motion sensor. But therefore you cannot catch the trigger-event from the motion sensor. Only the battery state. Someone said to pair only one light with the sensor and take the state of the light as a trigger. But this would mean, there is still on light with the behavior i don’t want.

Lets go on … Next try: get a zigbee module for my RPi and pair the motion sensor with the deConz system. Then let the sensor trigger all the time, like it did before, but forward only the trigger-events, which i want to put the lights on. This didn’t work, as deConz was disconnecting the sensor itself several times.

Well, my next try is about creating a “virtual light”, which should be paired with Tradfri gateway (i don’t even know, whether this is possible) and then being triggered by the motion sensor. The state of the virtual light should then be my trigger-even which i would work with to get the lights on and off.

Have someone tried such already and can give me some hints?

Do you have any other devices in deconz? If you only have wireless devices without any mains powered to extend the network you might need to keep it within a reasonable distance from the antenna.

There a a few things around …
But finnaly i got it … I had a RPi body out of metal, which was giving basicly the “you got 100% signal” condition, but will cause the connection to break up every few minutes. Never thought about this, as my WiFi adapter was outside the box. Replayed it by a plastic version now …

Well, im back to the “connetct motion sensor over deConz” solution.
This works fine, for now. I got a specified time, where the motion sensor triggert the lights for 30 seconds. And this works pretty well.

Still, my HA config checker is complaining …

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/automations.yaml”, line 1, column 3 expected , but found ‘-’ in “/home/homeassistant/.homeassistant/automations.yaml”, line 19, column 3

This is how my automation looks like:

- id: '1532005437294'
  alias: Flur AN durch Bewegung
  trigger:
  - entity_id: binary_sensor.tradfri_motion_sensor_
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.flur_2
    state: 'off'
  - after: '19:00:00'
    before: 01:00:00
    condition: time
  action:
    data:
      entity_id: light.flur_2
    service: light.turn_on
  - delay: '30'
  - data:
      entity_id: light.flur_2
    service: light.turn_off

- id: '1532259889216'
  alias: Ventilator AUS nach 1h
  trigger:
  - entity_id: switch.ventilator
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - delay: '3600'
  - data:
      entity_id:
      - switch.ventilator
    service: switch.turn_off

- alias: 'Flur AUS wenn vergessen'
  trigger:
    entity_id: light.flur_2
    from: 'off'
    platform: state
    to: 'on'
    for:
      hours: 1
      minutes: 00
  action:
    service: light.turn_off
    entity_id: light.flur_2

I see the checker is complaining about the “- delay: ‘30’” line, but i can’t see anything wrong here, especially as the rule works fine.

The first two rules were created over the GUI and work fine. The third one i created manually. To be honest … i didn’t get when i have to add the “-” and when i just format with spaces only …

PS: To explain:
1st: motion sensor should switch on light group for 30 seconds within the time of 7pm to 1am, only when it’s not turned on by hand before.
2nd: should switch off my fan 1h after i turn it on by hand (i turn it on, when i’m going to sleep)
3rd: should turn off light group 1h after it was turned on. Sometimes i just forget to turn it off and 1st rule didn’t work for this (on purpose) ^^

Since I never got along with yaml automations I use appdaemon instead since you use python instead. Have you tried an online yaml linter?

Thanks for the hint.
Never thought about looking for an online editor & validator ^^ But used one as you mentioned.
Here’s what i get as respond on the nfirst 3 i tried:

(< unknown >): did not find expected key while parsing a block mapping at line 1 column 3

ERROR:
while parsing a block mapping
in “< unicode string >”, line 1, column 3:
- id: ‘1532005437294’
^
expected < block end >, but found ‘-’
in “< unicode string >”, line 19, column 3:
- delay: ‘30’
^

Error : You cannot define a sequence item when in a mapping
Line : undefined undefined

So, i’m not really able to make a head or tail of this …
Seems like there is a problem with the id. And what is a “block end”, which seems to be missing there?

it’s the indenting and spaces…and, not sure, but there’s a trailing _ after the entity_id motion_sensor. That seems incorrect, or at least incomplete…
try:

- id: '1532005437294'
  alias: Flur AN durch Bewegung
  trigger:
    platform: state
    entity_id: binary_sensor.tradfri_motion_sensor_
    from: 'off'
    to: 'on'
  condition:
    - condition: state
      entity_id: light.flur_2
      state: 'off'
    - condition: time
      after: '19:00:00'
      before: 01:00:00
  action:
    - service: light.turn_on
      entity_id: light.flur_2
    - delay: 00:00:30
    - service: light.turn_off
      entity_id: light.flur_2
1 Like

After try and fail for a while i got it (and wanted to post it a bit earlier, in case someone has the same issue ^^):
i had to put a “-” in front of “data” in the “action” part.
Rest keeps the same, it works and hassibian does not complain any more.

But meanwhile i got another problem.
As mentioned the code keeps pretty much the same. But for better overview here it comes:

   - id: '1532005437294'
  alias: Flur AN durch Bewegung abends
  trigger:
  - entity_id: binary_sensor.tradfri_bewegungsmelder
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.flur_2
    state: 'off'
  - after: '19:00:00'
    before: 01:00:00
    condition: time
  action:
  - data:
      entity_id: light.flur_2
    service: light.turn_on
  - delay: '30'
  - data:
      entity_id: light.flur_2
    service: light.turn_off

I did an update to hassbian and deconz. This renamed my motion sensor device from “binary_sensor.tradfri_motion_sensor_” to “binary_sensor.tradfri_bewegungsmelder”. So it was just translated in german.
I noticed, that the automation was not working anymore, found this out and corrected the name in the automation rule.
There is nothing hassbian complains about! Also the rule seels to work first as i want, but now it ignores the condition

 - condition: state
      entity_id: light.flur_2
      state: 'off'

so i got the same thing, which i try to prevent: when i turn on the light by hand, it should not turn off, when the motion sensor recognizes movement. The automation rule should not be triggered at all, as the condition is not met, but it is :confused:
I turn on the light by hand, walk through the room and 30 seconds later the light turns off itself.

PS: the rule still follows the condition “only by the time of 19h to 01h”.
So i’m confused, why it only follows one condition, but not the other

You are not using the proper way of specifying that you have multiple conditions

  condition:
    condition: or
    conditions:

which would only be necessary for ‘or’ conditions.
Default is ‘and’ in which case one can simply list them, each condition having the ‘-’ in front.

btw, If you keep things tidy, automations become much easier to read.
For me that is beginning an action with the service, followed by the entity_id. If the service needs data to or from the entity_id, specify below the entity_id.

That would prevent the confusion here, about having to put a ‘-’ in front of the data field, which is not the case…

the last bit:

action:
  - data:
      entity_id: light.flur_2
    service: light.turn_on
  - delay: '30'
  - data:
      entity_id: light.flur_2
    service: light.turn_off

is much easier to read as follows:

action:
  - service: light.turn_on
    entity_id: light.flur_2
  - delay: 00:00:30
  - service: light.turn_off
    entity_id: light.flur_2

Note that for turning on/off a light, without further transitions or color settings, no data field is required.

same goes for the trigger…

  • entity_id: binary_sensor.tradfri_bewegungsmelder
    from: ‘off’
    platform: state
    to: ‘on’

would better be written as:

  -  platform: state
     entity_id: binary_sensor.tradfri_bewegungsmelder
     from: 'off'
     to: 'on'

see Automation Trigger - Home Assistant explaining the event is the trigger, on the various Platforms. List that first, then the entity_id, then the states being checked.

1 Like

Ty guys.
This file was generated my HoAs, as i created a rule over GUI, so i tried to change as less as possible to avoid errors.

Just one more thing (so far :smiley:) :
I want to add another “time” for the light to switch on/off in the morning.
Will the “lights are off AND it’s 19 to 01 OR it’s 05 to 07” condition work?
Will the system read it like “X AND (Y OR Z)” or like “(X AND Y) OR Z” so i would have to add another X like “(X AND Y) OR (X AND Z)”?

not sure… maybe try to formulate this in Yaml first, then let us help?

btw, what is your binary_sensor.tradfri_bewegungsmelder ? Is that a self made binary? I have many physical sensors here, but not Hassio sensors are made, only these:

24 No On/Off…

Since they’re connected to lights, and those lights do have on/off states, one can (and i have done so) read motion/presence from the light being On/Off, and one can create a Binary sensor template for that? Is that what you have done?

I didn’t creaty anything by hand.
I installed deConz and connected only the motion sensor directly to my Rapsberry over the Phoscon GUI, instead to the Tradfri gateway (for mentioned reasons).
After this i just started the HoAs GUI and the motion sensor was auto-discovered. It also was discovered twice. Once as " binary_sensor.tradfri_bewegungsmelder" and once as “binary_sensor.vpir_bewegung” (bewegungsmelder = motion sensor and bewegung = motion)
As both are doing the same, i porefered to youse the “motion sensor” thing before VPIR

Ah, that explains. I dont know any of these, is that a separate hub, or is it a software package? Or this RaspBee https://shop.dresden-elektronik.de/raspbee.html

Yep, this is this module with additional GUI, which looks like Home Assistant “lite”.
It got just basic settings and replaces the Gateway adding just a bit more functionality.
If you want, i can add some screenshots vor overview

please do so, id be most interested in what this can offer over the current tradfri integration in HA.

Give me a day :wink:
I’ll connect a light to deconz, so you can see the whole functions, as they were disabled with no lights connected.

@Mariusthvdb i rearranged my automations, so i think this looks easier to read now, just as you mentioned …

- id: '0101'
  alias: Flur ON
  trigger:
  - platform: state
    entity_id: binary_sensor.tradfri_bewegungsmelder
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: light.flur_2
    state: 'off'
  - condition: or
    conditions:
      - condition: time
        after: '19:00:00'
        before: '01:00:00'
      - condition: time
        after: '05:00:00'
        before: '07:00:00'
  action:
  - service: light.turn_on
    entity_id: light.flur_2
  - delay: 00:00:30
  - service: light.turn_off
    entity_id: light.flur_2

and also @Robban i guess i found the logical failure in my rule …
It’s not that easy and guess you could not see it.
My lights are called 1, 2 and 3 and they were summed up in a group “flur”. Also the group and the lights are arranged in Google Assistant in the room “flur”. BUT “light.flur_2” is only the group. So when i say "ok google, turn on “flur” google turn on all lights in the room “flur”, which means, it turns on 1, 2 and 3, but not the group element itself. This is why the rule keeps on triggering the lights, even i dont want it to.
I will rework this by tomorrow, so it wil take a look at 1, 2 and 3 to be all off, instead of the group.

nice.
If you want it even tidier do this:

instead of

trigger:
  - platform: state
    entity_id: binary_sensor.tradfri_bewegungsmelder
    from: 'off'
    to: 'on'

write:

  trigger:
    platform: state
    entity_id: binary_sensor.tradfri_bewegungsmelder
    to: 'on'

why? since you only have 1 trigger, you dont need the list '-' and since it is a binary_sensor, which is only On or Off, you can state only the to: or from: state, there’s no other option anyway :wink:

and you might want to change the id into something humanely understandable: simply use ‘Flur ON’ again.(you dont want to use the built-in editor anyway, so you might even leave it out completely…)

1 Like

It’s me again :smile:
I cleaned up the code a bit and optimized my structure. So it works now, but still not as i want it to…

  1. i tried different syntax, but found no working way except of listing lights 1, 2 and 3 as every own condition. maybe you know a nicer way?
  2. the rule is not triggered in the morning, but works fine in the evening. My code seems to be right. Maybe you see anything wrong here?
  3. as soon as i un-comment the data-brightness part, lights stay off. I can’t find a way to turn on lights by the brightness of like 10% (would be like 25 of 254). When commented out, they turn on by the brightness of last state. Any hints on this?

Can’t format code here, so another way: https://hastebin.com/utafexidah.pl

PS: @Mariusthvdb Got it: deConz short GUI / Frontend overview

nice! indeed useful extensions to the regular ikea interface, and on the browser… I only have an App, which stopped working because i use iOs12 beta…

so all you do is put the Raspbee on the Hassio Pi, and then everything is supposed to find each other? Or do you have the Raspbee on another Pi than your Hassio?

Please. could you screenshot some of the HomeAssistant things this Raspbee creates? Frontend maybe, and the yaml configuration so I can get a better idea of how these 2 systems work together?