#How to avoid an update when using now

1 messages · Page 1 of 1 (latest)

raven cedar
#

I have a template that includes the following
{{ ((now() - states.binary_sensor.mag_sensor.last_changed).total_seconds()/60)|round(0) }}
How can I output the minutes since last change when using this sensor without it updating every minute
I only what it to update when I call the template

fringe needle
#

If you define the template in yaml rather than the ui you can set a trigger for it which works just like an automation, so you can set it to be however you want to "call the template" - e.g. A button helper

raven cedar
#

Going to need some help, where are they stored?

fringe needle
#

wherever you define them to be stored in the configuration.yaml

raven cedar
#

Where would existing templates be located? I can find the yaml script for my existing UI automations and script

fringe needle
#

unless you have defined any in yaml before, there won't be any. templates defined in the ui as helpers are separate

raven cedar
#

Might be easier to explain what I have setup

I'm using a template as a function to return text
The template has the following state {{ 'open' if is_state('binary_sensor.mag_switch1', 'on') else 'closed' }} for {{ ((now() - states.binary_sensor.mag_switch1.last_changed).total_seconds()/60)|round(0) }} mins

I create a notification with the following Garage door {{states.sensor.garage_door_sensor.state}}
This generates a notification that looks like Garage door open for 30 mins for example

This works but the problem is the entity sensor.garage_door_sensor updates every minute because of now()
I dont even need it to store any data or any updates ever, I just want it to return updated text when the notification calls the template

OR am i going about this the wrong way and there is a better way to insert text
I would prefer to be able to send the entity as an argument

fringe needle
#

so you have a template helper with the blurb, something triggers an automation that sends a notification... why not just put that text generation template in the notification template directly?

raven cedar
#

So I can use the same code in multiple areas and for multiple entities, such as notification in home assistant and mobile notification

fringe needle
#

make a script then - send it a notification target, entity, and readable name as fields and have it construct it. it'll be more reusable (won't be tied to the garage door)
this isn't the kind of thing you should be using a template sensor for

#

i mean you can use a template sensor for it, then just exclude it from recorder and don't worry about it recalculating every minute because meh

raven cedar
#

So how would I call a script like in the above example to do something like
some text {{script(variable1, variable2)}} some more text
To return
some text data_from_script some more text
To then use in an action as data that calls a script

I can call a script like the following

action: script.send_notification
data:
  msg: "text {{entity}}"

But I'd like to get the result from entity and use it for different purposes

#

why is now even a forced trigger, isnt it a duplicate of a time based trigger

fringe needle
#

ok i think we need to take about a dozen steps back.
My understanding of what you want to do is:

  • because of some trigger (time open, time of day, whatever)
  • send a notification to (device/home assistant) that has some text in it based on how long a specific door has been open/closed
    is that accurate?
#

in general i would advise you to either: put the different purposes in the script, or construct it in a variable in the automation

#

now is a forced update because... when now is, constantly updates? any template sensor that uses now is one that should be expected to constantly update.

raven cedar
#

entity triggers, it then keeps looping every x number of minutes, repeatedly sending updated message until entity state changes.
The message is some dynamic text in this instance how long the entity has been in the state for e.g.
It just keeps sending message
Entity open for 10 minutes
Entity open for 20 minutes
Entity open for 30 minutes etc
The state and time are dynamic depending on what i put in the template
This message text is used in various places
By using a template I can use the same dynamic text in various actions

It currently works how I want, except I'd rather have it not record a new state every minute

fringe needle