Brightness of motion activated light dependent on bed occupancy

I recently setup a motion sensor in the hallway that only turns on at full brightness when activated. In lieu of a weight-centric bed occupancy sensor, I’m looking to manually flick an input_boolean each night so that the light turns on at lower brightness when motion detected. Here’s what I’ve come up with, but I’m getting errors:

- alias: Hall light dark motion on
  trigger:
  - domain: binary_sensor
    entity_id: binary_sensor.hall_motion_sensor
    platform: device
    type: motion
  condition:
  - below: '10'
    condition: numeric_state
    entity_id: sensor.hall_light_sensor
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.hall_light
      brightness_pct: >
        {% if is_state('input_boolean.bed_occupied', 'on') %}
          '10'
        {% else %}
          '100'
        {% endif %}

My input_boolean is configured as such:

input_boolean:
  bed_occupied:
    name: Bed is occupied
    icon: mdi:bed

In the templates tool, I get either ‘10’ or ‘100’ (in inverted commas), however, when the automation triggers, nothing happens.

Any ideas what I’ve done wrong?

Try removing the quotes around 10 and 100 and see if it works. Also check whether your light supports brighness_pct or only brightness.

Hi mate. It’s a philips hue dimmable globe running on deconz. Just tried brightness_pct on the services tool and it works with either quotes or without quotes.

When you trigger the automation manually, does it execute the desired action? Just for your info, when an automation is triggered manually it skips the trigger and conditions and goes straight to the action part, this way we can find out if the problem is within the action part.

I’ve triggered it manually and nothing happens, so it’s looking like an issue with the action. Also btw, no errors when check config is run.

Do you see any errors in the logs?

@Burningstone

Wow, finally worked it out, the brightness_pct values were indented too far in and when the quotes are removed it works…

- alias: Hall light dark motion on
  trigger:
  - domain: binary_sensor
    entity_id: binary_sensor.hall_motion_sensor
    platform: device
    type: motion
    device_id: d65097be65e741a5b9b0b13ee1f5c561
    # for:
    #   seconds: 1
  condition:
  - below: '10'
    condition: numeric_state
    entity_id: sensor.hall_light_sensor
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.hall_light
      brightness_pct: >
        {% if is_state('input_boolean.bed_occupied', 'on') %}
        5
        {% else %}
        100
        {% endif %}

Check it out!

Hmm, that’s strange, if I use multiline templates like this I have the same indentation then in your original example and it works.

Howerver I’m glad you got it to work :slight_smile:

Yeah, I was following the documentation on templates and that’s why I indented the way I did. I’m running 1.02.3, so maybe there’s a breaking change in there?

No there was no breaking change regarding this, just reread the release notes. I’m on the same version and don’t have this issue.

Weird, are you running hass.io? I am

Not in my production machine but on a test machine and there it works as well.

Something else to try because I’m pretty sure that the indentation shouldn’t have affected it and should have worked…I think.

try this as a test:

action:
  - service: light.turn_on
    data_template:
      entity_id: light.hall_light
      brightness_pct: >-
        {% if is_state('input_boolean.bed_occupied', 'on') %}
          5
        {% else %}
          100
        {% endif %}

notice the “-” after the > sign…

And just be aware that the config checker doesn’t check the validity of templates since you can put anything in there and it be potentially valid depending on the interpretation of the template at run time.

If the template doesn’t return proper yaml syntax then you will get an error in your home-assistant.log file when the automation tries to execute.

1 Like