Sensor template: setting state on blinking

Hello everyone,
i’ll searched around onterent the all day and i didn’t find anything usefull. except maybe a possible solution with node red.
I’m tryng to read the value of my gate lamp,
when is on is open
when is off is closed
and when blink it’s closing.

my template it’s like this for now:

  - sensor:
      - name: "State"
        state: >
          {% if is_state('binary_sensor.gate_channel_1_input', 'on') %}
            Closed
          {% else %}
             Open
          {% endif %}

i’m asking if i can implement a third state in case the lamp is blinking

Thank you all

does your gate tell home assistant (through a sensor) that the gate is blinking?

i have a “led spy” that act like
on if is open
off if is close
and blink if closing
i connect a shelly uni input channell to this led and i can read corrctly the value when it’s close or open, but i want also to determinate with a state if my gate is closing

maybe i express myself in a bad way: Blink mean that the led turn on and off several time untill the gate is close then it will fix on off

One way to do it would be with a triggered template sensor. I’m going to assume the lamp blinks once a second. I’ve made this more verbose than it needs to be so it is easier to understand.

There are three triggers, one that triggers as soon as the binary sensor state changes, and two that only trigger if the binary sensor has been on or off for longer than the lamp blink time.

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.gate_channel_1_input
        to:  # a null 'to' only triggers on all state changes, not attribute changes
        id: 'changed'
      - platform: state
        entity_id: binary_sensor.gate_channel_1_input
        to: 'on'
        for:
          seconds: 2  # make this slightly longer than the lamp blink time
        id: 'steady_on'
      - platform: state
        entity_id: binary_sensor.gate_channel_1_input
        to: 'off'
        for:
          seconds: 2  # make this slightly longer than the lamp blink time
        id: 'steady_off'
    sensor:
      - name: "Gate"
        icon: mdi:gate
        state: >
          {% if trigger.id == 'changed' %}
            Closing
          {% elif trigger.id == 'steady_on' %}
            Open
          {% elif trigger.id == 'steady_off' %}
            Closed
          {% else %}
            Unknown
          {% endif %}

Note: by “blink time” I mean time for the lamp to do a full on-off cycle. i.e. count the dark time as well as the light time for one cycle when the lamp is blinking.

1 Like

ty , i just implemented the code this morning i’ll be go to test harder in the next day but for the inital testing i’ve done, it seems to be perfect :slight_smile:
Only think i’ve to solve when restart ha, or shelly, state it’s stuck on unknow untill something happens on the gate

Try this:

template:
  - trigger:
      - platform: homeassistant
        event: start
        id: 'initialise'
      - platform: state
        entity_id: binary_sensor.gate_channel_1_input
        to:  # a null 'to' only triggers on all state changes, not attribute changes
        id: 'changed'
      - platform: state
        entity_id: binary_sensor.gate_channel_1_input
        to: 'on'
        for:
          seconds: 2  # make this slightly longer than the lamp blink time
        id: 'steady_on'
      - platform: state
        entity_id: binary_sensor.gate_channel_1_input
        to: 'off'
        for:
          seconds: 2  # make this slightly longer than the lamp blink time
        id: 'steady_off'
    sensor:
      - name: "Gate"
        icon: mdi:gate
        state: >
          {% if trigger.id == 'initialise' %}
            {{ 'Open' if is_state('binary_sensor.gate_channel_1_input', 'on') else 'Closed' }}
          {% elif trigger.id == 'changed' %}
            Closing
          {% elif trigger.id == 'steady_on' %}
            Open
          {% elif trigger.id == 'steady_off' %}
            Closed
          {% else %}
            Unknown
          {% endif %}

ok, it’s working like a charm thank you

HI, I used the same solution, but during opening the state change to Closing for the first 2 seconds and after change correctly to Open. Any solution for ignore the state for the first 2 seconds?