Automation to notify time period sensor state has been on or "1". Working

There is a sensor which normal state is 0. I would like an automation to notify time period sensor has been 1, after returning to 0. Something like “Sensor state is 0 again. It has been 1 for 20min”.
Could somebody point me how to easily do it? Of course, part I don’t know how to deal is how to get time period.

your first word is a hint

then show us what you have done

then we can point you down a path of no return LOL

most of the sensor have a Last Updated and or Last Changed

by going to the states page we can see it

then we can jump into the TEMPLATE page
do some jinja2

or you can just turn it on in lovelace

so write the automation the trigger

- id: Front Door Open
  alias: "Front Door Open"
  trigger:
  - entity_id: binary_sensor.front_door
    platform: state
    to: 'on'

remeber most thing have a on/off or true/false

a condition (this must be this)

  condition:
  - condition: state
    entity_id: sensor.day_night
    state: 'Night'

now the action bit

  action:
  - data_template:
      title: "Home Assistant"
      message: "{{ trigger.to_state.attributes.friendly_name }} was {% if trigger.to_state.state == 'on' %} Open {% else %} Closed {% endif %}"
    service: notify.stephan_phone

hope this start you down the path

2 Likes

It’s a about a water flow meter I use for irrigation control. Sonoff Mini with Espurna Firmware. It’s working grate. Espurna firm provides me two sensors: caudal event and caudal counter. I add another one to translate counter to litters/min which is irrelevant here.


When irrigation is on, caudal event turns to 1. When is off to 0.
I can´t see Last state or Last changed states on my sensors. I mean not only this one. No sensors (doors,fire, movement) have! Weird.

It can be related to no registry log. I don’t know why.

Please change your post’s category. “Share Your Projects” is for posts that share something you created with the community. Your post is a question about how to configure an automation so the “Configuration” category would be more appropriate. Thank you.

I think I’m on the way. I try to use “now()” in two different moments. I store first moment as variable “irrigation start”. key point is to recover this variable as time and not as tring. In spite of template use, I didn’t success yet. I combined as_timestamp, quotes, etc to not avail on message field.

alias: Prueba
description: ''
trigger:
  - platform: state
    entity_id: sensor.espurna_c8e7d5_caudal_event
    to: 'on'
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id: sensor.espurna_c8e7d5_caudal_event
        to: 'off'
  - service: notify.notificaciones_jemm
    data:
      message: 'Tiempo de riego: {{ now() - (irrigation_start) }}'
      title: Prueba
mode: single
variables:
  irrigation_start: now()

Please, could somebody give me a help?

Final automation in case it helps:

alias: Notifica riego realizado
description: Tras cada riego envía correo electrónico
trigger:
  - platform: state
    entity_id: sensor.espurna_c667d5_caudal_event
    from: '0'
    to: '1'
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id: sensor.espurna_c667d5_caudal_event
        from: '1'
        to: '0'
  - service: notify.gmail_sender
    data:
      message: |-
        La duración del riego ha sido-
        {%- set uptime  = as_timestamp(now())-irrigation_start | round -%} {%-
        set sep     = ', ' -%}
             {%- set TIME_MAP = {
                  'semana': (uptime / 604800) % 604800,
                  'día': (uptime / 86400) % 7,
                  'hora': (uptime / 3600) % 24,
                  'minuto': (uptime / 60) % 60,
                  'segundo': (uptime % 60)
              }
              -%}
              
         {%- for unit, duration in TIME_MAP.items() if duration >= 1 -%}
          {%- if not loop.first -%}
            {{ sep }}
          {%- endif -%}
            
          {{ (duration | string).split('.')[0] }} {{ unit }}

          {%- if duration >= 2 -%}
            s
          {%- endif -%}
        {%- endfor -%}

        {%- if uptime < 1 -%}
          ahora mismo
        {%- endif -%}
      title: Riego realizado
mode: single
variables:
  irrigation_start: '{{ as_timestamp(now()) }}'