Add time last changed to Python script

meaning dt is a fixed and standard defined object in Python and the ‘last_triggered’ attributes isnt the same?

could I try to set another variable, maybe choose my own, and use that?

still don’t understand why that would only cause errors at first run, and not later on though. This scripts is running all the time, and only throws these errors at boot, albeit in abundance …

Oh… that might be because the object may not have a last triggered datetime object on startup. Just perform a check to verify that it has the key inside the dictionary hass.states.get(‘automation.mode_selection’).attributes.has_key(‘last_triggered’)

great!
can i just add it to the existing script, any special place?

ive tested the my changed input_select.mode and it has
this I am looking for:
{{states.input_select.mode.last_changed}}

this the way in Python to select that:

dt = hass.states.get('input_select.mode.last_changed') ?

if that’s correct, why does this work fine:

dt = hass.states.get('automation.mode_selection').attributes.get('last_triggered')
#        if dt:
time = "%02d:%02d" % (dt.hour+1, dt.minute)

being: 2018-03-19 22:41:02.283549+00:00

and wont this work:

dt = hass.states.get('input_select.mode.last_changed')
#        if dt:
time = "%02d:%02d" % (dt.hour+1, dt.minute)

being: 2018-03-19 22:41:01.948863+00:00

complaining about unsupported operand type(s) for +: 'NoneType' and 'int'

still being unsure why the above makes a difference I modified the time declaration as follows:

dt = state.last_changed
dt = dt + datetime.timedelta(hours=+1)
time = "%02d:%02d" % (dt.hour, dt.minute)

which solves many error messages :+1:

This wont work because ‘input_select.mode.last_changed’ isn’t a key inside the states dictionary. I suggest you read up on python dictionarys.

i will certainly thanks. your post was the first mentioning has_key, and so i need to learn more on that .

anyways, the time declaration 1 post up does work.
most of the time.
thanks again, and if you notice any bits to be optimized : don’t hesitate…:wink: appreciate all the expert guidance i can get.

has_key is a dictionary method. You should take one of those free online coding python classes. Something like codecademy. It will teach you all the basics of python, dictionary work would be covered in that. You can get the tutorials done relatively quickly.

still searching for a robust solution to this timezone issue.

If i check this automation in dev-states it is clear the system thinks it is in Tzinfo: UTc:

hence the need to add +1 for all my time calculations.

How could I set the system in the correct Timezone, and do away with all other time issues based on that?

In my configuration.yaml the correct zone is mentioned, so I don’t really understand why these scripts wont use that?

Please assist me here,
Marius

at the top of your file:

from datetime import datetime, timezone

after the line with

dt = hass.states.get('automation.mode_selection').attributes.get('last_triggered')

add:

dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

This will only work in python, it will not work in the template editor.

1 Like

thanks!
will do.

  • this is needed for each dt = statement in the larger summary.py, cant set it globally in the script?
  • there is no way of telling the system to adapt this system wide, so a separate import for this (and other) python scripts even necessary.

I ask because it seems rather inefficient if one needs to state all over the system one is in a timezone, while a systemwide setting should take car of that…?

Cheers, and again thx!
Marius


doing do renders this error, before ive even added the other lines in the script.

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "summary.py", line 7, in <module>
ImportError: __import__ not found

Apparently HA doesn’t support imports with scripts… that’s kinda dumb. Anyways, you can probably just use the datetime object. I hope they have that imported into their environment.

dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)

remove the import too

well this is starting to be a bit of a nuisance, kinda dumb as you say.

either this:

or:

26

popping up.
Could it be anything else i need to set somewhere, cant imagine i am the only Hassio user encountering this problem… @mviezzer did you ever change the timezone settings? Why cant we import as @petro suggests? Manually adjusting the timezone seems rather ridiculous …

It’s because they are executing the python files with the exec() function. This is a limitation of that call. It’s possible to add the import built in but it can only be done on the interpreters end. I’m guessing there is a reason they avoid it.

As for your errors, did you change this line

dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

to this?

dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)

yes…

for entity_id in hass.states.get(lights_group).attributes['entity_id']:
    dt = hass.states.get('automation.sense_lights_change').attributes.get('last_triggered')
#    dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
    dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)
#    dt = dt + datetime.timedelta(hours=+1)
    time = '%02d:%02d' % (dt.hour, dt.minute)

gives:

08

welp, you are dead in the water then, just add your +1 and be done with it!

consider it done, and many thanks for your efforts, much appreciated!

btw, all this crap you are doing could be solved with appdaemon. You wouldn’t run into any of these issues.

1 Like

really? have no experience with that yet, next on the list it is then!

since all other methods proved impossible, i would like to try this.
How do i do the check? do i just drop it in the script, or should it be accompanied with several if, then else scenarios?
if has_key: continue
if ! has_key: do this
just asking…