Cant get my first automation to work

Hey there,
I startet out with Home Assistant a week ago. So far I got a couple of homemade esp8266 sensors and a zigbee2mqtt bridge with some lightbulbs and sensors connected to it running. Now I wanted to get my first automation working.

All I wanna do, is switch a Tradfri lightbulb on when I detect occupancy withe the Auara Motion sensor.

Here is my config:

binary_sensor:
  - platform: "mqtt"
    name: "anwesenheit_flur"
    state_topic: "zigbee2mqtt/melder_flur"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_on: true
    payload_off: false
    value_template: "{{ value_json.occupancy }}"
    device_class: "motion"
    json_attributes: 
      - "battery"
      - "voltage"


automations.yaml:

- alias: bewegungsmelder_flur_an
  trigger:
  - platform: state
    entity_id: binary_sensor.anwesenheit_flur
    from: "OFF"
    to: "ON"
  action:
    service: light.turn_on
    entity_id: light.tradfri_flur_2
    data:
      color_temp: 354
      brightness: 200


- alias: bewegungsmelder_flur_aus
  trigger:
  - platform: state
    entity_id: binary_sensor.anwesenheit_flur
    from: "ON"
    to: "OFF"
  action:
    service: light.turn_off
    entity_id: light.tradfri_flur_2

The binary sensor is already working, in the WebUI, i can see its state change. But I guess i messed up something with the automation. Can you guys help?

are you sure the state is “ON” and “OFF” and not “on” and “off”? (or On / Off )

Check in the states page.

also try ‘single quotes’ rather than “double quotes”

look in the “states” section (<>) of the dev-tools on side panel and see what the state of the motion detector is. Specifically, check if the state is reported in capital letters or lower case.

If it’s lower case then switch to that in your automation.

Also, just as an FYI, you don’t need to add the "from: " in the trigger if there are only two states possible with your device. Since you’re using a binary sensor then by definition it only has two states.

Thank you guys! You were both right. the state is reported in lower case letters. Adjusted my automations and now it works.

In general, yes, I would agree. But it’s still possible for a binary_sensor’s state to also be ‘unknown’ or ‘unavailable’, too. Depends on the sensor. If the sensor ever went from ‘unknown’ or ‘unavailable’ to ‘on’, then to: 'on' would trigger, whereas to: 'on' and from: 'off' would not. You’d have to decide which would work better for you in that case.

1 Like

good point