Trigger based on Light brightness change

So I jolted down my first YAML automation. It’s not working of course but hass doesnt give me any error when I restart it with this automation.

LocationSync:
  alias: "Update the location based on ST"
  trigger:
    - platform: state
      entity_id: light.virtual_security_dimmer
      attribute: brightness
      bt_pct_val: "{{ state_attr('light.virtual_security_dimmer', 'brightness')|int }}"
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.location_home
    - service: input_boolean.turn_off
      entity_id: input_boolean.location_away
    - service: input_boolean.turn_off
      entity_id: input_boolean.location_night
    - service: input_boolean.turn_off
      entity_id: input_boolean.location_disarm
    - service: input_boolean.turn_on
      data: {}
      entity_id: <
        {% if bt_pct_val == 0 %}
        input_boolean.location_disarm
        {% elif bt_pct_val == 25 %}
        input_boolean.location_home
        {% elif bt_pct_val == 75 %}
        input_boolean.location_night
        {% elif bt_pct_val == 100 %}
        input_boolean.location_away
        {% else %}
        input_boolean.location_none
        {% endif %}
  mode: single

I tried testing by changing values of light.virtual_security_dimmer via ST app and I could see that it is changed in HA as well, but that didn’t trigger this automation. Absolutely nothing in logs.

light.virtual_security_dimmer doesnt have brightness attributes(when I tried via UI in automation), could this be the reason that it’s not working? I can see that this dimmer is ON and set at 25% in HA though.

Any ideas?

Thanks.
Hemanshu

bt_pct_val is a variable and doesn’t belong to the trigger.
Did you not get any error under Configuration -> Logs Do you even see the automation under Developer Tools -> States.

Try changing it to this:

  alias: "Update the location based on ST"
  trigger:
    - platform: state
      entity_id: light.virtual_security_dimmer
      attribute: brightness
  action:
    - variables:
        bt_pct_val: "{{ state_attr('light.virtual_security_dimmer', 'brightness') | int }}"
    - service: input_boolean.turn_off
      entity_id: 
        - input_boolean.location_home
        - input_boolean.location_away
        - input_boolean.location_night
        - input_boolean.location_disarm
    - service: input_boolean.turn_on
      data:
        entity_id: >
          {% if bt_pct_val == 0 %}
            input_boolean.location_disarm
          {% elif bt_pct_val == 25 %}
            input_boolean.location_home
          {% elif bt_pct_val == 75 %}
            input_boolean.location_night
          {% elif bt_pct_val == 100 %}
            input_boolean.location_away
          {% else %}
            input_boolean.location_none
          {% endif %}
  mode: single

Also for this use case, an input_select with the 4 different options is the better choice instead of having multiple input_booleans, as the options are mutually exclusive.

Configuration logs was quite untill I stop and start hass(I was using pm2 restart hass till now).

Now I do see following error:

2|hass  | 2021-01-12 09:33:53 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: not a valid value for dictionary value @ data['action'][3]['entity_id']. Got None. (See /data/data/com.termux/files/home/.homeassistant/configuration.yaml, line 11). 

Automation YAML looks like this:

  alias: "Update the location based on ST"
  trigger:
    - platform: state
      entity_id: light.virtual_security_dimmer
      attribute: brightness
  action:
    - variables:
        bt_pct_val: "{{ state_attr('light.virtual_security_dimmer', 'brightness')|int }}"
    - service: notify.mobile_app_sm_n975u
      data:
        message: "Brightness percentage: {{ bt_pct_val }}"
    - service: input_boolean.turn_off
      entity_id: 
        - input_boolean.location_home
        - input_boolean.location_away
        - input_boolean.location_night
        - input_boolean.location_disarm
    - service: input_boolean.turn_on
      entity_id: <
        {% if bt_pct_val == 0 %}
        input_boolean.location_disarm
        {% elif bt_pct_val == 25 %}
        input_boolean.location_home
        {% elif bt_pct_val == 75 %}
        input_boolean.location_night
        {% elif bt_pct_val == 100 %}
        input_boolean.location_away
        {% else %}
        input_boolean.location_none
        {% endif %}
  mode: single

And no, I don’t see automations.LocaltionSync in developer tools -> state

Replace < with >

You should have an automation called automation.update_the_location_based_st.

When you go to Developer Tools -> States do you see an attribute brightness for your light?

Yikes, I’m sorry.
I did change it, but result is still same.

Logger: homeassistant.config
Source: config.py:415
First occurred: 9:48:50 AM (1 occurrences)
Last logged: 9:48:50 AM

Invalid config for [automation]: not a valid value for dictionary value @ data['action'][3]['entity_id']. Got None. (See /data/data/com.termux/files/home/.homeassistant/configuration.yaml, line 11).

Like this:

    - service: input_boolean.turn_on
      data:
        entity_id: >
          {% if bt_pct_val == 0 %}
      

1 Like

Or you could have copied my code 1:1 …

@Burningstone
Yes I do see brightness.

brightness: 128
friendly_name: Virtual Security Dimmer
supported_features: 33

@123

This one worked. I do see automation name under developer tools -> state.
I’ll give it a go with test now.

I still dont get it it…
in first - service call which turn off all 4 entities, we didn’t need data: but in second which does if else, we need data:

I guess there’s still a lot i need to learn.
thank you all for being so patient with me.

Test fails with following error (pm2 logs hass).


2|hass  | 2021-01-12 10:07:31 ERROR (MainThread) [homeassistant.components.automation.update_the_location_based_on_st] Update the location based on ST: Error executing script. Invalid data for call_service at pos 3: not a valid value for dictionary value @ data['entity_id']
2|hass  | 2021-01-12 10:07:31 ERROR (MainThread) [homeassistant.components.automation.update_the_location_based_on_st] While executing automation automation.update_the_location_based_on_st
2|hass  | Traceback (most recent call last):
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/components/automation/__init__.py", line 404, in async_trigger
2|hass  |     await self.action_script.async_run(
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1026, in async_run
2|hass  |     await asyncio.shield(run.async_run())
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 242, in async_run
2|hass  |     await self._async_step(log_exceptions=False)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 250, in _async_step
2|hass  |     await getattr(
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 457, in _async_call_service_step
2|hass  |     await service_task
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1399, in async_call
2|hass  |     processed_data = handler.schema(service_data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/validators.py", line 218, in __call__
2|hass  |     return self._exec((Schema(val) for val in self.validators), v)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/validators.py", line 340, in _exec
2|hass  |     raise e if self.msg is None else AllInvalid(self.msg, path=path)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/validators.py", line 336, in _exec
2|hass  |     v = func(v)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__
2|hass  |     return self._compiled([], data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
2|hass  |     return schema(data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__
2|hass  |     return self._compiled([], data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
2|hass  |     return base_validate(path, iteritems(data), out)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
2|hass  |     raise er.MultipleInvalid(errors)
2|hass  | voluptuous.error.MultipleInvalid: not a valid value for dictionary value @ data['entity_id']

I can see automation in developer tools -> state now.

 automation.update_the_location_based_on_st	on	last_triggered: 2021-01-12T15:07:31.687826+00:00
mode: single
current: 0
id: LocationSync
friendly_name: Update the location based on ST

When you supply additional options for a service call they should always appear under data: like this:

    - service: input_boolean.turn_off
      data:
        entity_id: 
          - input_boolean.location_home
          - input_boolean.location_away

However, there is one exception to the rule and that’s the commonly-used entity_id option when it contains no template. Consider it to be a ‘shortcut’ purely for convenience.

    - service: input_boolean.turn_off
      entity_id: 
        - input_boolean.location_home
        - input_boolean.location_away

However, if entity_id contains a template, the shortcut no longer applies and you must use data: as per the rule.

    - service: input_boolean.turn_on
      data:
        entity_id: >
          {% if bt_pct_val == 0 %}
          input_boolean.location_disarm
          {% else %}
          input_boolean.location_none
          {% endif %}

@123
Thank you. I have updated the automation as per your suggestion. It loads fine, no error but shows error while execution.
To execute/test this trigger, I’m manually changing virtual_security_dimmer value in ST app, which triggers and executes this automation. but it gives following error.

2|hass  | 2021-01-12 10:25:45 ERROR (MainThread) [homeassistant.components.automation.update_the_location_based_on_st] Update the location based on ST: Error executing script. Invalid data for call_service at pos 3: not a valid value for dictionary value @ data['entity_id']
2|hass  | 2021-01-12 10:25:45 ERROR (MainThread) [homeassistant.components.automation.update_the_location_based_on_st] While executing automation automation.update_the_location_based_on_st
2|hass  | Traceback (most recent call last):
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/components/automation/__init__.py", line 404, in async_trigger
2|hass  |     await self.action_script.async_run(
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1026, in async_run
2|hass  |     await asyncio.shield(run.async_run())
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 242, in async_run
2|hass  |     await self._async_step(log_exceptions=False)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 250, in _async_step
2|hass  |     await getattr(
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 457, in _async_call_service_step
2|hass  |     await service_task
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1399, in async_call
2|hass  |     processed_data = handler.schema(service_data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/validators.py", line 218, in __call__
2|hass  |     return self._exec((Schema(val) for val in self.validators), v)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/validators.py", line 340, in _exec
2|hass  |     raise e if self.msg is None else AllInvalid(self.msg, path=path)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/validators.py", line 336, in _exec
2|hass  |     v = func(v)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__
2|hass  |     return self._compiled([], data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
2|hass  |     return schema(data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__
2|hass  |     return self._compiled([], data)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
2|hass  |     return base_validate(path, iteritems(data), out)
2|hass  |   File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
2|hass  |     raise er.MultipleInvalid(errors)
2|hass  | voluptuous.error.MultipleInvalid: **not a valid value for dictionary value @ data['entity_id']**

Did you reload the autonations or restart HAafter the changes?

Before we dive deeper, which version of Home Assistant are you using?


EDIT

Also, what Burningstone said.

@Burningstone: Yes sir, I did both.
But found the issue.

the last service: turn_on call has only one entity_id based on if_else logic, so I don’t need to put - in front of that entity id name.
When I changed it, it started working. Tested few times and working fine.

Here’s final working copy.

- id: 'LocationSync'
  alias: "Update the location based on ST"
  trigger:
    - platform: state
      entity_id: light.virtual_security_dimmer
      attribute: brightness
  action:
    - variables:
        bt_pct_val: "{{ state_attr('light.virtual_security_dimmer', 'brightness')|int }}"
    - service: input_boolean.turn_off
      entity_id: 
        - input_boolean.location_home
        - input_boolean.location_away
        - input_boolean.location_night
        - input_boolean.location_disarm
    - service: input_boolean.turn_on
      data:
        entity_id: >
          {% if bt_pct_val == 0 %}
          input_boolean.location_disarm
          {% elif bt_pct_val == 64 %}
          input_boolean.location_home
          {% elif bt_pct_val == 191 %}
          input_boolean.location_night
          {% elif bt_pct_val == 255 %}
          input_boolean.location_away
          {% else %}
          input_boolean.location_none
          {% endif %}
  mode: single

Thank you so much everyone.

Where was that shown in your example above?
I see this:

    - service: input_boolean.turn_on
      entity_id: <
        {% if bt_pct_val == 0 %}
        input_boolean.location_disarm
        {% elif bt_pct_val == 25 %}
        input_boolean.location_home
        {% elif bt_pct_val == 75 %}
        input_boolean.location_night
        {% elif bt_pct_val == 100 %}
        input_boolean.location_away
        {% else %}
        input_boolean.location_none
        {% endif %}

Home Assistant 2020.12.2

Running on older Android phone with termux. Running Node-red and mosquitto as well.

Integrated ST with HA as first step of migrating away(still using zwave and zigbee radios from ST and probably continue using until i’m moving my HA on raspberry pi).

I was pasting it as I was copying, I’m not sure why it’s not showing on code snippet. I apologize for wasting your time. You guys would have spotted the issue way sooner.

A cardinal rule when requesting assistance is to provide accurate information. If you show us one thing but use something else, we are left to work with faulty information.

Yes, we would have noticed the superfluous hyphen immediately.

Good luck.