Templating - a plea for help

I am one of the worst at formatting yaml templates. I am trying to create/update an input.time_to_go with a state value of (line 1/line 2 ) * line 3 below as part of an automation action. The base automation is:

- id: '1593546221327'
  alias: calculate_time_remaining
  trigger: 
  - entity_id: sensor.maverick_temp2  
    platform: state
  action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.time_to_go
        value:  ???
[ {{ state('input.smoker_target') }} - {{ state(sensor.maverick_temp2) }}

/ {{ trigger.to_state.state }} - {{ trigger.from_state.state }} ]

* {{ as_timestamp(now()) - as_timestamp(states.sensor.maverick_temp2) }}

ultimately, I would like the value to be in minutes.

Is there a kind soul out there who would help me format the syntactically correct entry that I should put after value:

Okay, so:

  1. It’s states, not state.
  2. Everything you want to template goes into the {{ }}, all other stuff is literal.
  3. Quotes are necessary.

Is sensor.maverick_temp2 a sensor of time, heat, or what?

Thanks…

It’s a temperature sensor. I am trying to find the elapsed time between the last sensor change and the current one.

How does this look?

value: "{{ 
               ( 
                  (
                    (states('input.smoker_target')  -  states(sensor.maverick_temp2))  / 
                    (trigger.to_state.state  -  trigger.from_state.state)
                  ) *
                    (as_timestamp(now() - as_timestamp(states.sensor.maverick_temp2.last_updated)
                  )
             )
          }}"

If you want that you only need

{{ (now() - states.sensor.maverick_temp2.last_updated).total_seconds() }}

Thanks…

I’m actually trying to estimate "time to reach target temperature…when will we be ready to eat.

((difference between target temp and current temp) / (difference between last temperature reading and current reading) ) * (time between last temperature reading and current temperature reading).

If the thermometer always moved one degree between readings, then this would be the logic:

If my (target temperature is 140F and the current thermometer temperature is 100F). it should take roughly 40 times the (time it took to go up one degree) to reach the target temperature.

However, the thermometer isn’t that consistent and when it changes (and triggers the automation, it jumps two degrees…so it would take only 20 times the interval time…thus the slightly more complicated formula.

This does not work. I need to know the number of seconds between the last trigger and the current trigger events. I have tried many different options and just haven’t stumbled on the right incantation.

Well then that’s hard, because HA doesn’t provide access to stuff like that in Jinja. After all Jinja’s supposed to be a lightweight programming language, and Python’s supposed to do the hard work. Try a python_script.

don’t know how to do that…I am programming an automation and need to find the number of seconds between the last time the automation was triggered and the current time it was triggered. Surely there is a way to do this…

{{ (now() - states.automation.my_thing.last_updated).total_seconds() }}
 action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.time_to_go

        value: "{{ 
                 ( 
                     (now() - states.automation.my_thing).total_seconds()
                 )          
                }}"
,,,

 log shows:

 error executing script. unexpected error for call_service at pos 1: unsuppported operand type(s) for -:'datetime.datetime' and 'NoneT...

I fixed it, see edit.

some progress…maybe. First, I fixed the automation.my_things :slight_smile:

Used your latest version and I am getting values posted…but not seconds…value is:

0.019775 or 0.017748 ???

I think you’ll need to trigger another script and get the info for that script, since the current automation was just triggered.

or save

states.automation.my_thing).total_seconds()

in an input_number

and then do something like

now() - state.input_number

????

I’m confused what you mean.
You have an automation, and you check the last script’s trigger time. If it’s far away enough, call the script again.

I’ll try that tomorrow. Thanks for your help and pointers.