Trigger and Condition with ID:

trigger:
  - platform: numeric_state
    id: 'on'
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level
    below: '175.0'
  - platform: numeric_state
    id: 'off'
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level
    above: '275.0'
condition:
  - condition: trigger
    id: 'on'
  - condition: state
    entity_id: light.front_porch
    state: 'off'
  - condition: trigger
    id: 'off'
  - condition: state
    entity_id: light.front_porch
    state: 'on'

Attempting to use this code to trigger a light on and off depending on the trigger id:, then using the appropriate condition: trigger, but I can’t seem to get it right. Prior to the introduction of condition:trigger in HA I had this broken out into 2 automations, one for turning the light on, another for turning it off.

I looked for an example in the docs, but could not find anything.

Thanks in advance!

1 Like

You want to combine 2 automation into a single one?

You can try using this-

trigger:
  - platform: numeric_state
    id: 'on'
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level
    below: '175'
  - platform: numeric_state
    id: 'off'
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level
    above: '275'

action:
  - service: >-
      light.turn_{{ 'on' if trigger.id == 'on' else 'off' }}
    target:
      entity_id: light.front_porch

Using the automation above, light will be turned ON if light level is below 175. (This will not check the state of the light (ON/OFF) before executing the action.

Another alternative if you want to add condition of the state of light prior to executing the action-

trigger:
  - platform: numeric_state
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level
    below: '175'
    id: 'ON'
  - platform: numeric_state
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level
    above: '275'
    id: 'OFF'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 'ON'
          - condition: state
            entity_id: light.front_porch
            state: 'off'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.front_porch
      - conditions:
          - condition: trigger
            id: 'OFF'
          - condition: state
            entity_id: ''
            state: light.front_porch
            for: 'on'
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.front_porch
    default: []
1 Like

Also these conditions will never allow your automation actions to run. Conditions are AND logic by default. You can not have the light on and off at the same time. You can’t have both triggers being true at the same time either.

condition:
  - condition: trigger
    id: 'on'
  - condition: state
    entity_id: light.front_porch
    state: 'off'
  - condition: trigger
    id: 'off'
  - condition: state
    entity_id: light.front_porch
    state: 'on'

I think what you wanted was:

condition:
  - condition: or
    conditions:
      - condition: and # porch light on and trigger id off
        conditions:
          - condition: state
            entity_id: light.front_porch
            state: 'on'
          - condition: trigger
            id: 'off'
      - condition: and # OR porch light off and trigger id on
        conditions:
          - condition: state
            entity_id: light.front_porch
            state: 'off'
          - condition: trigger
            id: 'on'
1 Like

Thank you both! My example was only to show what I wanted to do, not that is would necessarily work.

Please don’t post code you know won’t work.

If you can’t work out why it won’t work, fair enough post that. But deliberately posting code you know is not correct without fixing it first just wastes everyone’s time.

2 Likes

I was using it for an example of what was working as 2 individual automations. :slight_smile: Sorry for all the grief.

This would never work, as I explained:

condition:
  - condition: trigger
    id: 'on'
  - condition: state
    entity_id: light.front_porch
    state: 'off'
  - condition: trigger
    id: 'off'
  - condition: state
    entity_id: light.front_porch
    state: 'on'

Should it be trigger.id? I’ve had problems with that and instead used trigger.to_state.state. The below works better for me:

alias: Occupancy - Basement Equipment Room
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.occupancy_basement_equipment_room
condition: []
action:
  - service: light.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }}
    target:
      entity_id: light.hue_overhead_light
mode: single

(and note, I prefer to use room occupancy variables - then multiple things and trigger those on or off, then have automations trigger based on that, rather than having a sensor directly trigger an automation)

Thanks for this example. It was exactly the issue I was trying to resolve today.

1 Like