Adjustable countdown timer to switch off any entity

I follow the layout of the example “Adjustable countdown timer to switch off any entity for dashboard”
In this I have replaced the word “office” with “ZB2”
In this example, an entity “Office timer Remaining” is created.
This does not work, the entity “ZB2 Timer Remaining” cannot be found.
Could it be the template?

The template is in the “templates.yaml” folder

Template :
# ZB2 countdown timer to switch of any entity
 sensors:
 ZB2_time_remaining:
 friendly_name: 'ZB2 Time Remaining'
 unit_of_measurement: "min"
 value_template: >
 {% if is_state('input_boolean.zb2_time_starter', 'on') %}
 {{ [ (states('input_number.ZB2')|int - (as_timestamp(now()) - as_timestamp(states.input_boolean.zb2_time_starter.last_changed))/240)|round(0) ,0 ] | max }}
 {%else%}
 0
 {%endif%}

My configuration.yaml states:

/homeassistant/configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
front end:
 themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
sensor: !include_dir_merge_list sensors
scene: !include scenes.yaml
template: !include templates.yaml

#Lovelace multiple card
Lovelace:
 resources:
 - url: /local/multiple-entity-row.js?v=4.5.0
 type: module
 id: '1704728852787'
  alias: zb2 timer
  description: adjust zb2 light lime
  trigger:
    - platform: state
      entity_id:
        - sensor.zb2_time_remaining
      to: "0"
  condition: []
  action:
    - service: light.turn_off
      data: {}
      target:
        entity_id: switch.zb2_switch
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.zb2_timer
  mode: restart

Can anyone help me??

You may first want to fix the indent of your template because that’s quite off (and it’s template, not Template):

template:
  # ZB2 countdown timer to switch of any entity
  sensors:
    ZB2_time_remaining:
      friendly_name: 'ZB2 Time Remaining'
      unit_of_measurement: "min"
      value_template: >-
        {% if is_state('input_boolean.zb2_time_starter', 'on') %}
          {{ [ (states('input_number.ZB2')|int - (as_timestamp(now()) - as_timestamp(states.input_boolean.zb2_time_starter.last_changed))/240)|round(0) ,0 ] | max }}
        {%else%}
          0
        {%endif%}

Correct that first and see if it changes anything.

It doesn’t work because its configuration is invalid. There are two distinct ways to define a Template Sensor in YAML (modern and legacy) and you have mixed them together.

I suggest you re-write the configuration exclusively in modern format (refer to the documentation).

It’s worse than that.

template: !include templates.yaml

already includes the template: header and shows that this is a modern-format template sensor, yet it’s written in legacy format.

Is it possible ChatGPT is responsible, or is this just a case of blindly copying something from years ago without understanding it?

Try this:

- sensor:
    - name: "ZB2 time remaining"
      unit_of_measurement: "min"
      state: >
        {% if is_state('input_boolean.zb2_time_starter', 'on') %}
          {{ [ (states('input_number.zb2')|int - (as_timestamp(now()) - as_timestamp(states.input_boolean.zb2_time_starter.last_changed))/240)|round(0) ,0 ] | max }}
        {% else %}
          0
        {% endif %}

I haven’t attempted to diagnose any issues with the template itself (like what the 240 represents), but it should at least load now even if it doesn’t work as expected. The only change i made was to put the entity ID in lowercase.

Alternatively, you could create the sensor via the UI, under Helpers / Template. Use the template above: the bit between (and including) the first {% and the last %}.

Your configuration.yaml also contains errors, which I can’t believe load up properly. Have you re-typed it rather than copy/paste?!

front end:
Lovelace:
1 Like

Many thanks for yuour swift repy

Thanks, I can now find the entity “ZB2 time remaining”.
but the automation is not working yet. Have I done something wrong in the automation?

id: '1704728852787'
  alias: zb2 timer
  description: adjust zb2 light lime
  trigger:
    - platform: state
      entity_id:
        - sensor.zb2_time_remaining
      to: "0"
  condition: []
  action:
    - service: light.turn_off
      data: {}
      target:
        entity_id: switch.zb2_switch
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.zb2_timer
  mode: restart

What is the state of sensor.zb2_time_remaining in Developer Tools / States? Is it 0 or 0.0?

Try:

  trigger:
    - platform: state
      entity_id: sensor.zb2_time_remaining
      to: "0.0"

  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.zb2_time_remaining
      below: 1
1 Like

in all cases status sensor.zb2_time_remaining= 0

- id: '1704728852787'
  alias: zb2 timer
  description: adjust zb2 light lime
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.zb2_time_remaining
          {{ states ('sensor.zb2_time_remaining') }}
          {{ states ('sensor.zb2_time_remaining') == "0" }}
          {{ states ('sensor.zb2_time_remaining') == "0.0" }}      
  condition: []
  action:
    - service: light.turn_off
      data: {}
      target:
        entity_id: switch.zb2_switch
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.zb2_timer
  mode: restart

It is still not working

I didn’t mean you to type those templates into the trigger — they were for testing in the template editor and what you now have is completely invalid.

Use Taras’ suggestion.

Sorry for the mistake made. I am not very proficient in Home Assistant. the original text has been restored
The answer is :
Sensor.zb2_time_remaining
0
True
False

You’ve used light.turn_off on a switch entity. You really should use the UI rather than editing YAML — it would prevent such basic errors.

Should be switch.turn_off, but please use the UI to build the automation — just change to YAML mode to copy code out to paste here.

I worked the automation in the UI

Still not working. Do you have a clou ?

- id: '1704728852787'
  alias: zb2 timer
  description: adjust zb2 light lime
  trigger:
  - platform: state
    entity_id:
    - sensor.zb2_time_remaining
    to: '0'
  condition: []
  action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: switch.zb2_switch
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.zb2_timer
  mode: restart

Try what I suggested in my previous post.

Still using light.turn_off on a switch…

- id: '1704728852787'
  alias: zb2 timer
  description: adjust zb2 light lime
  trigger:
    - platform: numeric_state
      entity_id: sensor.zb2_time_remaining
      below: 1
  condition: []
  action:
    - service: switch.turn_off
      data: {}
      target:
        entity_id: switch.zb2_switch
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.zb2_timer
  mode: restart