Set variable in choose not supported?

Hi, triggered by a motion sensor and constrained by some conditions I want to set a light to 1% or 100% brightness based on the time of day. For that I chose to use variables and the choose action. However I cannot get it to work.

The automation is also available on my repo.

  - id: d3fb887c-c15d-46a0-8b28-4edbecab9613
    alias: Bad Bewegungsmelder
    description: Control the bathroom lights with motion triggers
    mode: restart
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.presence_lower_bathroom
        from: "off"
        to: "on"
    condition:
      - condition: and
        conditions:
          - condition: or
            conditions:
              - condition: sun
                after: sunset
              - condition: sun
                before: sunrise
          - condition: or
            conditions:
              - condition: state
                entity_id: light.lower_bathroom_yeelight
                state: "off"
              - condition: state
                entity_id: input_boolean.bathroom_motion_trigger_turned_on_light
                state: "off"
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.bathroom_motion_trigger_turned_on_light
      - choose:
          - conditions:
              - condition: time
                after: "01:00:00"
                before: "07:00:00"
            sequence:
              - variables:
                  brightness_pct: 1
        default:
          - variables:
              brightness_pct: 100
      - service: light.turn_on
        data:
          entity_id: light.lower_bathroom_yeelight
          brightness_pct: "{{ brightness_pct }}"
      - delay:
          seconds: "{{ states('input_number.bathroom_motion_trigger_delay') | int }}"
      - service: light.turn_off
        data:
          entity_id: light.lower_bathroom_yeelight
      - service: input_boolean.turn_off
        entity_id: input_boolean.bathroom_motion_trigger_turned_on_light

My variable brightness_pct is in fact empty when I the step light.turn_on is triggered after the choose step.

The whole stacktrace:

Logger: homeassistant.components.automation.bad_bewegungsmelder
Source: core.py:1405
Integration: Automation (documentation, issues)
First occurred: November 25, 2020, 11:53:39 PM (4 occurrences)
Last logged: 9:07:17 AM

While executing automation automation.bad_bewegungsmelder
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 433, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1010, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 245, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 460, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1405, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 218, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 340, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 336, in _exec
    v = func(v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 340, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 338, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected float for dictionary value @ data['brightness_pct']

Is setting variables inside choose not supported or am I doing something wrong?

Thank you!

No, it’s not supported.

As a point, trying to use it in that way is just making a really simple issue very complicated…

action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.bathroom_motion_trigger_turned_on_light
  - service: light.turn_on
    data:
      entity_id: light.lower_bathroom_yeelight
      brightness_pct: "{{ '1' if now().hour in [1, 2, 3, 4, 5, 6] else '100' }}"
  - delay:
      seconds: "{{ states('input_number.bathroom_motion_trigger_delay') | int }}"
  - service: light.turn_off
    entity_id: light.lower_bathroom_yeelight
  - service: input_boolean.turn_off
    entity_id: input_boolean.bathroom_motion_trigger_turned_on_light

Thank you for the fast answer and thank you even more for the advice!

1 Like