Automation: trying to combine "platform: state" with "if, then, else"

Hi, i have setup an automation with 2 or more switches and lights to turn on/off using “platform: state”.
I would like to combine this with “if, this, else” but i have some trouble with the yaml code.

My goal is to automatically turn on the lights at 50% after sundown/before sunset when one of the switches are used.

This is the yaml without dimming the lights. I use this for all our lights/switches and it works perfect.

alias: test
description: ""
trigger:
  - platform: state
    entity_id:
      - light.testlamp
      - switch.sonoff_10008c4c7a_1
    from: "on"
    to: "off"
  - platform: state
    entity_id:
      - light.testlamp
      - switch.sonoff_10008c4c7a_1
    from: "off"
    to: "on"
condition: []
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id:
        - switch.sonoff_10008c4c7a_1
  - service: light.turn_{{ trigger.to_state.state }}
    target:
      entity_id: light.testlamp
mode: restart

I tried this yaml code but that doesn’t work:

alias: test
description: ""
trigger:
  - platform: state
    entity_id:
      - light.testlamp
      - switch.sonoff_10008c4c7a_1
    from: "on"
    to: "off"
  - platform: state
    entity_id:
      - light.testlamp
      - switch.sonoff_10008c4c7a_1
    from: "off"
    to: "on"
condition: []
action:
  - if:
      - condition: time
        after: "23:00:00"
        before: "07:00:00"
    then:
      - service: switch.turn_{{ trigger.to_state.state }}
        target:
          entity_id:
            - switch.sonoff_10008c4c7a_1
      - service: light.turn_{{ trigger.to_state.state }}
        data:
          brightness_pct: 50
        target:
          device_id: 3b4720b2c4952ac4747d9b4b97682e62
    else:
      - service: switch.turn_{{ trigger.to_state.state }}
        target:
          entity_id:
            - switch.sonoff_10008c4c7a_1
	  - service: light.turn_{{ trigger.to_state.state }}
        data:
          brightness_pct: 100
        target:
          device_id: 3b4720b2c4952ac4747d9b4b97682e62
mode: restart

Can someone point me in the right direction here please?

You haven’t accounted for the “off” side of your triggers in the actions. I would also organize it a little differently.

alias: test
description: ""
trigger:
  - platform: state
    entity_id:
      - light.testlamp
      - switch.sonoff_10008c4c7a_1
    from: 
      - "on" 
      - "off"
    to:  
      - "on" 
      - "off"
condition: []
action:
  - choose:
    - conditions:
        - condition: template
          value_template: "{{ trigger.to_state.state == 'off'}}"
      sequence:
        - service: homeassistant.turn_off
          target:
            entity_id: 
              - light.testlamp
              - switch.sonoff_10008c4c7a_1
    default:
      - service: switch.turn_on
        target:
          entity_id: switch.sonoff_10008c4c7a_1
      - service: light.turn_on
        data:
          brightness_pct: "{{50 if (now().hour >= 23 or now().hour < 7) else 100}}"
        target:
          entity_id: light.testlamp
mode: single

Thanks for the quick reply. I never used a ‘template’ before.
The code works but the lamp doesn’t seem to dim to 50%. I changed the hours to a different value for testing but it always turns on to 100% brightness.

[edit] i used the same code on a different light and it works. Somehow the hue-bulb doesn’t seem to co-operate. The Sonoff-light works.

Is it possible to use this with the ‘sunset’ and sunrise’ value with an offset of 1 hour? I tried replacing ‘hour’ with ‘sunset/sunrise’ but no luck.