So I am doing some math with the timestamp from a calendar and now(). I setup a couple of Template Sensors to do the math, but the resulting value is not updating.
the sensor.now_seconds updates every couple seconds in the States UI, and the sensor.next_meal_seconds updates whenever the google calendar updates.
The only one that doesn’t seem to update until I restart Home Assistant is sensor.next_meal_diff_seconds
I am running the hass.io install (version 0.61.1) in an Ubuntu Server 16.04 VM.
You have parenthesis around the ( stuff | int ). Not sure if thats necessary, and it may cause issues because you are placing a tuple into something that isn’t expecting a tuple. Could be wrong… but try this instead:
next_meal_diff_seconds:
value_template: "{{ (as_timestamp(states.calendar.pte_meal_plan_recipes.attributes.start_time)-as_timestamp(now())) | int }}"
So removing the parenthesis around the (stuff|int) didn’t change anything.
But this is interesting, I created another sensor that uses the first two sensors to do the math and that one updates every time sensor.now_seconds updates. This technically resolves my issue, but it’s freaking weird that I have to daisy chain sensors like that to do math.
Without knowing the api, my guess is that as_timestamp() returns strings and each one would need to be converted to a int in order to add/subtract them:
value_template: "{{ as_timestamp(states.calendar.pte_meal_plan_recipes.attributes.start_time) | int -as_timestamp(now())| int }}"
If that’s not the case, then I don’t know why it wouldn’t work.
I know much more about this crap than I did in January. The reason none of @Chris_Garcia’s original templates are updating is because template sensors only update when the entity’s inside the template update. now() is not considered an entity, so his templates will only update when states.calendar.pte_meal_plan_recipes.attributes.start_time update. To skirt around this issue, you can use entity_id config option for the template sensor to assist the update process. Each entity you attach will cause the value_template to be updated when that entity state changes.
How do you do this please @petro ? Very useful thread by the way!
EDIT: don’t worry, I figured it out from @proddy 's post!
EDIT 2: actually, this history charts for this template sensor come out as categorical, even though the maths is clearly happening… How do I make them numerical so that I get a line chart of the history?
add a unit of measure to it and it will treat it like a number instead of a string. You could also force it to an integer or float by using the int/float filter in jinja2.