Automating Switch off all lights when Mains Power goes off

Hi All

Hoping or some help.
I have EmonCMS measuring current into my HA dash including the built-in Energy Component.
Works flawlessly.
We have bad loadshedding here in South Africa (when they turn off power grids to free up demand on the electricity network, more consumption than output capacity).
I Have my lights all integrated and am able to control them perfectly.

I am trying to automate that when the main power in (Wh) either = 0 or >5 then switch off all lights.
I cant find the correct trigger, have tried all the options I can think of.

The Mains Power Entity reports the Wh as a variable STATE.

Here is my last attempt in YML:

alias: "Light off when loadshedding "
description: Turn off all lights when mains power goes off
trigger:
  - platform: state
    entity_id:
      - sensor.emoncms_mains
    to: "0"
condition: []
action:
  - service: light.turn_off
    data: {}
    target:
      area_id:
        - entrance
        - guest_bathroom
        - kids_room
        - kitchen
        - living_room
        - veranda
mode: single


Any help would be great....
type or paste code here

I believe this is what you are looking for:

alias: "Light off when loadshedding "
description: Turn off all lights when mains power goes off
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.emoncms_mains
    below: 5
condition: []
action:
  - service: light.turn_off
    data: {}
    target:
      area_id:
        - entrance
        - guest_bathroom
        - kids_room
        - kitchen
        - living_room
        - living_room
        - veranda
mode: single

You’re a Rockstar my friend, worked first time. Thank you.

I’m glad it worked out for ya.