Help with sensor Template, four ELIF states

Hi!

I have created an template for showing three diffrent states for my robotic mower.
It consist of a timer and an binary sensor that senses if the mower is docked in the station or not.

The code looks like this: (Binary ON = Mowing)

  - platform: template
sensors:
  status_am:
    value_template: >-
      {% if states.binary_sensor.opencloseam.state == 'on' %}
        Mowing
      {% elif states.timer.chargning_am.state == 'idle' %}
        Docked
      {% else %}
        Chargning
      {% endif %}

If the mower is NOT inte the docked position = Mowing
If the mower recently came back to the station = Charging (always takes 1,5h)
If the mower have been in the station for 1,5h it is Docked (and waitinig)

I also want to add the stage “Stuck”, if the binary sensor have been in the “on” position (Mowing) for more than 3h. The mower is probably stuck somewhere.

I am not sure how to do this. I was thinking somehing like (code below) this but I do not know how to use AND criterion inside templates lite this

  - platform: template
sensors:
  status_am2:
    value_template: >-
      {% if states.binary_sensor.opencloseam.state == 'on' %}
        Mowing
      {% elif is_state("timer.mowtime_am", "idle") and
  is_state("binary_sensor.opencloseam", "on") -%}
        Stuck
      {% elif states.timer.mowing_am.state == 'idle' %}
        Docked
      {% else %}
        Charging
      {% endif %}

The timers works like this:

- alias: status-am
  trigger:
platform: state
entity_id: binary_sensor.opencloseam
to: 'off'
  action:
   - service: timer.start
 entity_id: timer.charging_am

.

- alias: status-am-stuck
  trigger:
platform: state
entity_id: binary_sensor.opencloseam
to: 'on'
  action:
   - service: timer.start
 entity_id: timer.mowing_am

Maybe:

  - platform: template
    sensors:
      status_am2:
        value_template: >
          {% if is_state('binary_sensor.opencloseam', 'on') %}
            {% if is_state('timer.mowtime_am', 'idle') %}
              Stuck
            {% else %}
              Mowing
            {% endif %}
          {% elif is_state('timer.mowing_am.state', 'idle') %}
            Docked
          {% else %}
            Charging
          {% endif %}
1 Like

Thank you so much!!!
Had to do some minor changes but otherwise it seems to work flawless :slight_smile:

This is my final code:

sensor:

- platform: template
  sensors:
    status_am2:
      value_template: >
        {% if is_state('binary_sensor.opencloseam', 'on') %}
          {% if is_state('timer.mowtime_am', 'idle') %}
            Stuck
          {% else %}
            Mowing
          {% endif %}
        {% elif is_state('timer.charging_am', 'idle') %}
          Docked
        {% else %}
          Charging
        {% endif %} 

automation:

- alias: status-am
  trigger:
    platform: state
    entity_id: binary_sensor.opencloseam
    to: 'off'
  action:
   - service: timer.start
     entity_id: timer.charging_am

- alias: status-am-stuck
  trigger:
    platform: state
    entity_id: binary_sensor.opencloseam
    to: 'on'
  action:
   - service: timer.start
     entity_id: timer.mowtime_am

timer:

chaging_am: 
  duration: 01:10:00
  
mowtime_am:
  duration: 02:20:00

One problem tho, if I restart HA the sensor will always show “stuck” due to timers getting also getting reset.

Maybe there’s a way to create an automatic start of the timers when HA is restarted

- alias: status-am
  trigger:
    - platform: state
      entity_id: binary_sensor.opencloseam
      to: 'off'
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: binary_sensor.opencloseam
      state: 'off'
  action:
   - service: timer.start
     entity_id: timer.charging_am

- alias: status-am-stuck
  trigger:
    - platform: state
      entity_id: binary_sensor.opencloseam
      to: 'on'
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: binary_sensor.opencloseam
      state: 'on'
  action:
   - service: timer.start
     entity_id: timer.mowtime_am

Clever! I have this implemented now :slight_smile: