Automation Failing

Hello, my first question, I am stuck getting this automation to work. I am new to HA and have limited experience but I have tried to alter it below from 0.01p to 0.001p etc but I do not know what I am doing wrong. When Agile falls below then nothing triggers. Can anyone help direct me please?

# Half Hourly Octopus Agile Electricity rate
sensor:
  - platform: rest
    name: agile_electric_cost
    scan_interval: 86400
    resource_template: "https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-N/standard-unit-rates/?period_from={% set ts_now = ((as_timestamp(utcnow())/1800)|round(0,'floor')|int * 1800) %}{{ ts_now |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ', False) }}&period_to={{ (ts_now + 1800) |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ', False) }}"
    value_template: '{{ (value_json.results[0].value_inc_vat) | round(2) }}'
    unit_of_measurement: 'p/KWH'

  - platform: time_date
    display_options:
      - 'time'

automation:
  - alias: Update Octopus Agile Price
    description: ''
    trigger:
    - hours: '*'
      minutes: '30'
      platform: time_pattern
      seconds: '0'
    - hours: '*'
      minutes: '0'
      platform: time_pattern
      seconds: '0'
    action:
    - data:
        entity_id: sensor.octopus_energy_cost_tracker_agile
      service: homeassistant.update_entity

  - alias: Turn on Battery & Heating
    trigger:
      platform: numeric_state
      entity_id: sensor.octopus_energy_cost_tracker_agile
      below: 0.01
    action:
      service: homeassistant.turn_on
      entity_id:
        - switch.livingroom_heater
        - switch.bedroom_heater
        - switch.hallway_heater
        - switch.battery_storage
        
  - alias: Turn off Battery & Heating
    trigger:
      platform: numeric_state
      entity_id: sensor.octopus_energy_cost_tracker_agile
      above: 0.02
    action:
      service: homeassistant.turn_off
      entity_id:
        - switch.livingroom_heater
        - switch.bedroom_heater
        - switch.hallway_heater
        - switch.battery_storage

Point 11 here How to help us help you - or How to ask a good question

Well that was easy, thank you, hopefully someone can guide me now as it what’s wrong with the code. :blush:

If your units are really pence per kilowatt hour (kWh to capitalise it correctly), you are pretty much asking it to only switch on when the Agile price goes negative.

What is the current value of the Agile sensor?

Thanks, I’m not sure where to find this…

Far from being an expert, but at first glance, it seems round(2) is your problem. You won’t get granularity from 0.01 to 0.001 unless you change that to round(3)

Agreed — but the main problem is that the Octopus API returns costs as pence per kWh, but the value in the automation suggests that the OP has written it to be in £ per kWh.

If that’s really meant to be below 1p then above 2p per unit, it should be 1 and 2 not 0.01 and 0.02.

Thank you, il change this and see how I get on, many thanks.

Hello, I have tried changing the settings within the code, eg, 0.01, 0.1, 1, 1.00 but it does not trigger when the price falls below, If I run the automation directly then it works as it should but wont trigger on price. Thanks

You have a bit of a strange setup for updating. The sensor you have updating once a day (sensor interval) but then you have an automation with a bit of a wacky time pattern to force an update… Looks like it’s trying to do so every 30 mins?

what if you change the scan_interval to 1800?

id first do that and make sure that the sensor actually updates … if it does then we can make sure the automation triggers

That’s normal for the Agile sensor. You want it to update just after the half hours, as that’s when the values are published.

1 Like

Has the price fallen below 1p/kWh? Show the history graph of the sensor. You’re grasping at straws without that data.

The entity ID in your Rest sensor does not match the one in your automation. Have you just copy/ pasted a load of code? If the config above is everything, you’re asking the automation to trigger off a non-existent sensor.

So i checked the sensor, when I manually turn it on it works so its not the swtiches, but from this picture, the agile rate does not update as it should do and I guess this is the issue,

It’s one of the issues. The other main one is that your numeric state triggers are watching sensor.octopus_energy_cost_tracker_agile and I don’t think you even have that sensor.

I’m completely stuck, just giving up on it now, I really dont have the knowledge to find whatever it is is wrong (despite extensive time spent), but thanks for your time and input anyway. :slight_smile:

Sounds like you need to start with something a bit more basic to build your understanding of how HA works.

Copy-pasting swathes of code from separate topics and expecting it to all work together well only end in disappointment.

2 Likes

Now I’m in front of a real computer (rather than replying on a phone) let me try to give a bit more help. I think this can be all fixed with one change to the code.

At the very start of your code, you have:

sensor:
  - platform: rest
    name: agile_electric_cost
    scan_interval: 86400

That sets up sensor.agile_electric_cost.

However, the automations that do the half-hourly updating and the switching on and off are both looking at sensor.octopus_energy_cost_tracker_agile which, as far as I can tell, doesn’t exist on your system.

So change the name line in the code above to read:

sensor:
  - platform: rest
    name: Octopus Energy cost tracker Agile
    scan_interval: 86400

Next make sure your automations’ below and above lines to something that makes sense for pence per unit, perhaps below: 1 and above: 2 or even higher for testing.

Then restart your system (or do a YAML reload if it asks you).

1 Like

Thanks for your patience, I will give this a go later tonight when I get back in from work, I will obviously change the values to mirror the current rates at that time to see if it works.
Many Thanks.

sensor:
  - platform: rest
    name: Octopus Energy cost tracker Agile
    scan_interval: 86400
    resource_template: "https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-N/standard-unit-rates/?period_from={% set ts_now = ((as_timestamp(utcnow())/1800)|round(0,'floor')|int * 1800) %}{{ ts_now |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ', False) }}&period_to={{ (ts_now + 1800) |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ', False) }}"
    value_template: '{{ (value_json.results[0].value_inc_vat) | round(2) }}'
    unit_of_measurement: 'p/KWH'

  - platform: time_date
    display_options:
      - 'time'

automation:
  - alias: Update Octopus Agile Price
    description: ''
    trigger:
    - hours: '*'
      minutes: '30'
      platform: time_pattern
      seconds: '0'
    - hours: '*'
      minutes: '0'
      platform: time_pattern
      seconds: '0'
    action:
    - data:
        entity_id: sensor.octopus_energy_cost_tracker_agile
      service: homeassistant.update_entity

  - alias: Turn on Battery & Heating
    trigger:
      platform: numeric_state
      entity_id: sensor.octopus_energy_cost_tracker_agile
      below: 20
    action:
      service: homeassistant.turn_on
      entity_id:
        - switch.livingroom_heater
        - switch.bedroom_heater
        - switch.hallway_heater
        - switch.battery_storage
        
  - alias: Turn off Battery & Heating
    trigger:
      platform: numeric_state
      entity_id: sensor.octopus_energy_cost_tracker_agile
      above: 30
    action:
      service: homeassistant.turn_off
      entity_id:
        - switch.livingroom_heater
        - switch.bedroom_heater
        - switch.hallway_heater
        - switch.battery_storage

Hi, I’ve tried this tonight, with values 16, 0.16, 0.0.16 but still does not trigger automatically, I’ve attached some screenshots so you can see my basic setup, not sure what could be wrong…