(*
  POU: fb_light
  Single light control: HA ON/OFF + Zigbee toggle. Priority: HA OFF > HA ON > Zigbee.
*)
FUNCTION_BLOCK fb_light
VAR_INPUT
    ha_on: BOOL;
    ha_off: BOOL;
    zigbee_sw: BOOL;
    relay_status: BOOL;
END_VAR
VAR_OUTPUT
    light_output: BOOL;
    light_status: BOOL;
END_VAR
VAR
    r_trig_ha_on: R_TRIG;
    r_trig_ha_off: R_TRIG;
    r_trig_zigbee: R_TRIG;
    light_state: BOOL := FALSE;
END_VAR

// Implementation
r_trig_ha_on(CLK := ha_on);
r_trig_ha_off(CLK := ha_off);
r_trig_zigbee(CLK := zigbee_sw);

IF r_trig_ha_off.Q THEN
    light_state := FALSE;
ELSIF r_trig_ha_on.Q THEN
    light_state := TRUE;
ELSIF r_trig_zigbee.Q THEN
    light_state := NOT light_state;
END_IF;

light_output := light_state;
light_status := relay_status;
