Python is going to crush me - generic thermostat

Not really sure where this should be posted. I am rapidly learning - that is rapidly getting over my head - :slight_smile:

So I put together a Tasmota based furnace controller with some embedded temperature and gas sensors. That is all okay - I understand the electronics. I got the controller working with Home assistant and got the generic thermostats working. All good. I then started to set up the automation when I came across a python script that had been created that did the scheduling. It didn’t work for me at first.

The first issue was that the automation that ran it had code to run the python script every 10 minutes but simply didn’t work. After some poking around I discovered it was using the time platform with a “/10” attribute that wasn’t right. It appears that home assistant now uses a time_pattern platform. Made that change it worked.

Now the python script works and runs every 10 minutes to check what the schedule uses. The issue I am having is that the last part of the python script does not work. That part of the script sends a notification when the temperature changes. That part of the script is calling something called ‘notifications_send’ which appears to be some other script as it is not part of home assistant. I know zip about python nor how to use python in home assistant so I match the perfect definition on newbie in this case. I’m a real script kiddie so to speak.

I have changed the ‘notifications_send’ to notify.notify but it doesn’t work for me. The parameters seem right to me but no go. Does anyone have an idea on what I should do next (besides start my on line course on python coding - that is next year!) The original code segment is:

new_temp = TEMP_HIGH if in_high_time else TEMP_LOW 
if new_temp != current_temp:
    # set the thermostat target temperature.
    hass.services.call('climate', 'set_temperature', {'entity_id': climate_entity, 'temperature': new_temp})

    # also send a notification so that I know that it changed the temperature.
    hass.services.call('script', 'notifications_send', {'title' : 'HA: Changing thermostat', 
        'message': 'Changing temperature to {} (was at {})'.format(new_temp, current_temp)})

Note the hass.services.call(‘climate’, part works fine.

I tried modifying to:

new_temp = TEMP_HIGH if in_high_time else TEMP_LOW 
if new_temp != current_temp:
    # set the thermostat target temperature.
    hass.services.call('climate', 'set_temperature', {'entity_id': climate_entity, 'temperature': new_temp})

    # also send a notification so that I know that it changed the temperature.
    hass.services.call('script', 'notify.notify', {'title' : 'HA: Changing thermostat', 
        'message': 'Changing temperature to {} (was at {})'.format(new_temp, current_temp)})

Note: notify.notify works perfectly fine when I just call it in an automation. My phone lights up in seconds.

Thanks for any help

I’m treading lightly here since I haven’t done this kind of HA scripting but I think it should be something like call('notify', 'notify'. In other words, the service you’re calling is notify and not script. In an automation the syntax is service.method but here it’s call(service, method.

Thanks that did it. Yes it does say ‘script’ in that code segment. So I changed it to the following and it is working perfectly:

    # also send a notification so that I know that it changed the temperature.
    hass.services.call('notify' , 'notify', {'title' : 'HA: Changing thermostat', 
        'message': 'Changing temperature to {} (was at {})'.format(new_temp, current_temp)})

For anyone wanting to use this the entire code is:

WEEK_SCHEDULE = [
    [datetime.time( 0, 0), datetime.time( 1, 0)],     # from 00:00 to 01:00
    [datetime.time( 6, 0), datetime.time( 21, 0)]     # from 07:00 to 09:00
]
WEEKEND_SCHEDULE = [
    [datetime.time( 7, 0), datetime.time( 11, 0)],
    [datetime.time(16, 0), datetime.time(21, 0)]
]

TEMP_HIGH = 23.0
TEMP_LOW  = 15.5

climate_entity = 'climate.house_heating' # set to your thermostat entity
current_temp = hass.states.get(climate_entity).attributes['temperature']

now = datetime.datetime.now().time()
if datetime.datetime.now().date().weekday() < 5:
    current_schedule = WEEK_SCHEDULE
else:
    current_schedule = WEEKEND_SCHEDULE

in_high_time = False
for high_time in current_schedule:
    start = high_time[0]
    end = high_time[1]
    
    if start <= now <= end:        
        in_high_time = True
        break

new_temp = TEMP_HIGH if in_high_time else TEMP_LOW 
if new_temp != current_temp:
    # set the thermostat target temperature.
    hass.services.call('climate', 'set_temperature', {'entity_id': climate_entity, 'temperature': new_temp})

    # also send a notification so that I know that it changed the temperature.
    hass.services.call('notify' , 'notify', {'title' : 'HA: Changing thermostat', 
        'message': 'Changing temperature to {} (was at {})'.format(new_temp, current_temp)})
1 Like