Automation ignoring Input Datetime trigger

Hello everyone

Problem description:
I have an automation which I want to run during the night but the time at which it should run will be different each night.

Current test implementation:
The automation has an Input Datetime helper as its trigger. I run some python code using pyscript to set the Input Datetime helper to a datetime value in the future and then wait for the time to occur but the automation is not run.

But if I use the Home Assistant UI to manually enter a datetime value into the helper and then wait for the time to occur the automation triggers as required.

Why does the automation ignore the datetime if it is set using python code?

Background/Why am I doing this:
My electricity is supplied by Octopus energy and I am on their Agile tariff which means that the cost of my electricity varies every half hour. I have solar panels, a hybrid inverter and batteries. In winter (negligible sunshine) I want to use my inverter to charge my batteries using the cheapest half hour slots of electricity. Each day Octopus publish the price of electricity for the following 24 hours at approximately 4pm. I have calculated that I need 4 hours (8 half hour slots) of charging to fully charge my batteries overnight assuming they are nearly flat each day.

I have an automation which is set to run everyday at 5pm. It finds the cheapest eight half hour slots for the following day, these will almost always be during the night.

If I could simply toggle my inverter on and off to charge the batteries I would not have a problem, but it does not work like that.
The inverter API accepts a package of data containing up to three pairs of start times and end times and will then work its way through them switching the battery charging on and off as necessary.

The interesting bit is matching up 8 half hours slots with 3 start and end times. There is no guarantee that the half hours run consecutively. My python code starts at the beginning of the earliest slot and merges consecutive half hours together until it finds a gap and then closes the first charge period with the end of the half hour it has reached. It can repeat this three times in all. If I am lucky it will have managed to include all half hour slots, if not the worst case would by just three half hour charge periods.
This automation stores the three charging period start and end times to a six Input Datetime helpers.

Another automation runs at 5:15pm, it picks up the values from above and links to the Inverter using its API to assign the values.

So this is all currently implemented and works reasonably well.

The enhancement I am working on extends the code above to continue working through the half hour slots to create a maximum of six charging periods if the half hour slots are particularly fragmented. My plan is to wait until the inverter has completed the charging of the batteries using the first three charging periods and then update it again with the charging schedule for another three periods. The timing of this is critical, the update needs to be sent to the inverter after the third charging period has ended but before the fourth charging period begins. This can be calculated and is stored to an Input Datetime helper.

Now we get to my problem, the automation I first mentioned is supposed to run at the datetime calculated above and its job is to send the fourth, fifth and sixth charging periods to the inverter. At the moment it ignores the trigger Input Datetime helper and fails to run.

Show us the “pyscript” code and the automation code, properly-formatted for the forum; and the state of the input_datetime that is being ignored.

I assume you’re aware that others have worked on this topic before, myself included? Monster thread here:

Thank you for your reply. I am aware of other work on this topic and should thank BottlecapDave for his Octopus Energy integration, hultenvp for SolisSensor and stevegal for solis_control. Thanks to them I have managed to implement a working solution for reading from Octopus and writing to my Solis inverter.

I explain what I was trying to do in detail in order to save people from having to ask me “Why do you want to do that?”

As far as I can tell my problem is simply to do with setting up an automation and updating a time trigger using pyscript. So I have created a simple test with four parts to illustrate the problem.
I have tried passing the datetime value in many different formats and with different wait intervals, the code below is just one example.

  1. Set up an Input Datetime helper, I called mine: Battery Charge Time 2

  2. Create a simple python program:


from datetime import datetime, timedelta

def set_helper() -> str:
    input_datetime.battery_charge_time_2 = (datetime.now() + timedelta(minutes=1)).strftime("%d/%m/%Y %H:%M")

    retVal = "Dummy"
    return retVal

@service   
async def call_set_helper():
    a_string = set_helper()

  1. Create an automation to call this program which assigns a value to the Input Datetime helper:
    (I keep this automation disabled and run it manually)

alias: Automation_One
description: ""
triggers:
  - minutes: /30
    trigger: time_pattern
conditions: []
actions:
  - action: pyscript.call_set_helper
    data: {}
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: >-
        Automation one has set helper value to {{
        states('input_datetime.battery_charge_time_2') }} at {{
        now().strftime("%d/%m/%Y %H:%M:%S") }}.
mode: single

  1. Create an automation to be triggered when the correct time is reached:
    (To save waiting, in this example I have set the pyscript program to assign this to be now() plus 1 minute. This automation is enabled)

alias: Automation_Two
description: " "
triggers:
  - trigger: time
    at: input_datetime.battery_charge_time_2
conditions: []
actions:
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: >-
        Automation two has been fired at {{ now().strftime("%d/%m/%Y %H:%M:%S")
        }}.
mode: single

It is my experience that this all appears to work - the first automation does call the pyscript code, the pyscript code does update the Input Datetime helper. But the second automation never fires when the trigger time is reached.

You should use the input_datetime.set_datetime() service call (with %Y-%m-%d %H:%M:%S format) rather than directly trying to set the state.

Pyscript: calling services
Input datetime service call

Thanks again to Troon for your excellent advice.
I’m pleased to say that my implementation is now working correctly.
I struggled a bit with the syntax required for the service call but got there in the end.

1 Like