The rest of the sensor is a binary_sensor and likely not of use to you. My input_datetime.garbage should be equivalent to your sensor.greenbin. The state format for an input_datetime is YYYY-MM-DD HH:MM:SS. If sensor.greenbin does not match that then that’s the problem.
If your current code works for Google calendar then you need to investigate the state difference between sensor.greenbin and a Google calendar date. Likely there is a state format difference that is preventing it from working with your current code.
Does that work @petro ? Because the documentation says it only works for days in the past.
relative_time converts datetime object to its human-friendly “age” string. The age can be in second, minute, hour, day, month or year (but only the biggest unit is considered, e.g., if it’s 2 days and 3 hours, “2 days” will be returned). Note that it only works for dates in the past .
Right, that’s for the macro template method (mixing up work software and HA). I’m referring to hui-relative-time which is used throughout the UI to display device_class timestamp sensors. Assuming that @dicko will be using this in the UI in an entities field, it should return a correct time frame in the past or future.
I think the reason its not working for me is that is that the sensor is been created in Node-Red and the sensor is being classed as a string. Nothing seems to pick up what is coming through. Ive done more digging and its a EPOCH Number and the future date I am now counting down to is 1590447600 which is Tuesday 26th May. Im only after full days and do not require the hours and minutes. I can change the string format in Node-Red but it doesnt seem to matter either as I have tried.
Thanks and sorry if I didnt get all the info in the first place. This has been doing my head in for 2 days now.
Hi. Im at a loss. I can get the date to leave Node Red without formatting to be EPOCH like I said above. I can use the moment node and change that to be what ever date format for the sensor in Home Assistant.
Ive searched the internet and also searched these forums and tried many solutions for trying to get it converted into days to go but nothing. the lovelace card shows unknown, unavailable and Ive even had invalid date format.
Ive also tried your code above too and below. I have 2 entities/sensors green_bin and green_bin_2 trying to get this working but my main sensor will be green_bin
- platform: template
sensors:
green_bin_countdown_web:
friendly_name: Green Bin Countdown WEB
device_class: timestamp
entity_id:
- sensor.green_bin_2
- sensor.date_time
value_template: >
{{ (as_timestamp(now()) - as_timestamp("states.sensor.green_bin_2") ) | timestamp_custom("%j")| int }}
also tried this too but nothing that says days remaining
- platform: template
sensors:
green_bin_countdown:
friendly_name: Next Green Bin Collection in
entity_id:
- sensor.green_bin
- sensor.date_time
value_template: >
{% set bin_time = states('sensor.green_bin') %}
{% set daystogo = (as_timestamp(now()) - as_timestamp(collection_date)) /86400 %}
{% if daystogo < 1.0 %}
Today
{% elif daystogo < 2 %}
Tomorrow
{% elif daystogo < 28 %} in {{ ((daystogo+1)/2)|int }} days
{% else%}
UNKNOWN
{% endif %}
Take a look at people grabbing state information above.
now compare that to what you have
There are 2 method to grabbing state information.
State method.
states('sensor.green_bin_2')
From the State Object
states.sensor.green_bin_2.state
You wrapped your state object in quotes turning it into a string, which is a mixture of both methods. You should be using 1 or the other and you shouldn’t mix the two.
If you use the state object method, you shouldn’t use quotes around it anywhere. It’s an object, when you add quotes around it, you turn it into a string.
Thanks for all your help. I managed to get it working this morning but 1 more question.
I now have 2 sensors for the same item sensor.green_bin and sensor.green_bin_2
sensor.green_bin shows the epoch time of 1590447600
sensor.green_bin_2 shows the same date as 2020-05-26 00:00:00
I need the green_bin_2 date format for the script to work below but is there any chance to maybe bring it in as the epoch time and convert to the above date ?
Id also like to show the date as Tuesday 26th May 2020 on an entity card.
is there a way to do all this from 1 sensor rather than 2 or 3 ?
SO, the above script has been working great at counting down and even showed TOMORROW up yesterday on the countdown but today, it should have shown TODAY but it is now showing unknown.
Why UNKNOWN ? I have tried changing from == 0.0 to <= 0.0 as I can see its less that 0.0 from another code in developer tools
well, it’s hard to follow your logic with all your in line conversions. I’d personally write it this way because it’s way easier to understand. This should work.
Thanks for that, I will keep this in mind in future but it didnt work. I had various errors when trying to implement. 1st was the the line below. I could not get valid code till I changed the =< to <=
{% if bin_time =< midnight %}
Once I did that, the code was valid but still would not show in the developers tab, so I tried the code on one of my bin sensors and it still showed unknown.
I don’t think you should mark that as a solution. You’re calling double methods & multiple conversions in multiple spots and you have unused variables.
As for your template, if you are dead set on using it at least fix it so that others who come into this thread have a cohesive solution without errors & unnecessary conversions littered throughout it.