Rest Sensor receiving JSON - Astralpool - connectmypool.com.au

Thanks, guys. I’m going to have a crack at configuring it now.

How long did it take you to get the api? I’ve registered for it through the web portal and haven’t heard anything back.

Thanks @zagnuts I got the API but I am unsure where to paste the code and which one to actually paste since there are a number of revisions.

We have:

  • Pool / Spa Heater
  • Water Feature - Valve Control
  • Water Feature 2 - Valve Control
  • Pool Pump
  • Spa Pump
  • Lights
  • PH reader etc…

Can you give me a hint on which of the above code I need and where I paste this in HA?

Thanks so much!

How did you get the power use of the pool heater?

I think it was the next day. Given that it’s done manually, and with the lockdown(s), it might take a bit longer at the moment.

Ah - it is in a few places. So to make it a little clearer, here are the basics. Firstly, you need to get the API from connectmypool. Then you need to add that info into your configuration.yaml to grab the raw JSON data

  - platform: rest
    name: Pool Status
    resource: https://www.connectmypool.com.au/api/poolstatus
    method: POST
# Change the API to match yours. You might want to put this in your secrets file.
    payload: '{ "pool_api_code": "XXXXXX-1234567" }'
    force_update: true
    json_attributes:
    - pool_spa_selection
    - heat_cool_selection
    - temperature
    - active_favourite
    - heaters
    - solar_systems
    - channels
    - valves
    - lighting_zones
    value_template: 'OK'
    scan_interval: 120
    verify_ssl: true
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json

To access this info, you then need to pull out the JSON data to create individual sensors, eg:

  - platform: template
    sensors:
      pool1_heat_cool_selection:
        friendly_name: Pool - Heat Cool Selection
        value_template:   >-
          {{ ['Cooling', 'Heating'][states.sensor.pool_status.attributes["heat_cool_selection"]|int] }}
      pool1_temperature:
        friendly_name: Pool - Current Temperature
        value_template: '{{ states.sensor.pool_status.attributes["temperature"] }}'
        device_class: temperature
        unit_of_measurement: '°C'
      pool1_active_favourite:
        friendly_name: Pool - Active Favourite
        value_template: '{{ states.sensor.pool_status.attributes["active_favourite"] }}'
      pool1_heaters0_mode:
        friendly_name: Pool - Heater Mode
        value_template:   >-
          {{ ['Off', 'On'][states.sensor.pool_status.attributes["heaters"][0]["mode"]|int] }}
      pool1_heaters0_settemp:
        friendly_name: Pool - Heater set temperature
        value_template:   >-
          {{ states.sensor.pool_status.attributes["heaters"][0]["set_temperature"]|int }}
        device_class: temperature
        unit_of_measurement: '°C'
      pool1_channels0_mode:
        friendly_name: Pool - Current Channel Mode
        value_template:   >-
          {{ ['Off', 'Auto', 'On', 'Low Speed', 'Medium Speed', 'High Speed'][states.sensor.pool_status.attributes["channels"][0]["mode"]|int] }}
      pool1_valves0_mode:
        friendly_name: Pool - Valve Mode
        value_template:   >-
          {{ ['Off', 'Auto', 'On'][states.sensor.pool_status.attributes["valves"][0]["mode"]|int] }}
      pool1_light_status:
        friendly_name: Pool - Light Status
        value_template:   >-
          {{ ['Off', 'Auto', 'On'][states.sensor.pool_status.attributes["lighting_zones"][0]["mode"]|int] }}

Note that I haven’t bothered grabbing everything - I don’t have a separate spa for example. If you do, then you will likely want to add those in. So now you should be able to access the status info from within HA such as what is the current water temp. To change any settings, there are various ways to do that but firstly you need to create a rest_command as follows:

rest_command:
  poolaction:
    url: "https://www.connectmypool.com.au/api/poolaction"
    method: post
    content_type:  'application/json; charset=utf-8'
# Change the API to match yours. You might want to put this in your secrets file.
    payload: '{"pool_api_code": "XXXXXX-1234567","action_code": {{action_code}},"device_number": {{device_number}},"value": "{{value}}","wait_for_execution": false}'
    verify_ssl: true

Then you can create individual switches for specific devices eg the following creates one to turn the heater on/off:

  - platform: template
    switches:
      pool_heater:
        value_template: "{{ is_state('sensor.pool1_heaters0_mode', 'On') }}"
        friendly_name: Pool Heater Switch
        turn_on:
         - service: rest_command.poolaction
           data: 
             action_code: 4
             device_number: 0
             value: 1
         - delay: 2
         - service: homeassistant.update_entity
           entity_id: sensor.pool_status
        turn_off:
         - service: rest_command.poolaction
           data: 
             action_code: 4
             device_number: 0
             value: 0
         - delay: 2
         - service: homeassistant.update_entity
           entity_id: sensor.pool_status

Optionally you can do what I did which is to use input_select to make a nice drop down selection from Lovelace. It’s a bit time consuming to do, but I think worth it:

input_select:
  pool_lights_mode:
    name: Pool Lights Mode
    options:
      - "off"
      - "auto"
      - "on"
    icon: mdi:dome-light
  pool_light_colour:
    name: Pool Lights Colour
    options:
      - "Ocean"
      - "Red"
      - "White"
      - "Blue"
      - "Disco"
    icon: mdi:palette
  pool_set_temp:
    name: Pool Set Temp
    options:
      - "18"
      - "28"
      - "38"
    icon: mdi:coolant-temperature
  pool_set_fave:
    name: Pool Set Favourite
    options:
      - "Daily_Filter"
      - "All_Off"
      - "All_Auto"
    icon: mdi:heart-plus-outline

and in my automations.yaml I have:

- id: '1628215649699'
  alias: Pool Lights on
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_lights_mode
    to: 'on'
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 6
      device_number: 0
      value: 2
  - delay: 2
  - service: homeassistant.update_entity
    entity_id: sensor.pool_status
  mode: single
- id: '1628216257083'
  alias: Pool Lights auto
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_lights_mode
    to: auto
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 6
      device_number: 0
      value: 1
  - delay: 2
  - service: homeassistant.update_entity
    entity_id: sensor.pool_status
  mode: single
- id: '1628216485771'
  alias: Pool Lights off
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_lights_mode
    to: 'off'
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 6
      device_number: 0
      value: 0
  - delay: 2
  - service: homeassistant.update_entity
    entity_id: sensor.pool_status
  mode: single
- id: '1628218549063'
  alias: Pool Temp to 18
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_set_temp
    to: '18'
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 5
      device_number: 1
      value: 18
  - delay: 2
  mode: single
- id: '1628219245273'
  alias: Pool Temp to 28
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_set_temp
    to: '28'
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 5
      device_number: 1
      value: 28
  - delay: 2
  mode: single
- id: '1628219608541'
  alias: Pool Temp to 38
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_set_temp
    to: '38'
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 5
      device_number: 1
      value: 38
  - delay: 2
  mode: single
- id: '1628219940723'
  alias: Pool Colour Ocean
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_light_colour
    to: Ocean
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 7
      device_number: 0
      value: 17
  - delay: 2
  mode: single
- id: '1628220258739'
  alias: Pool Colour White
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_light_colour
    to: White
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 7
      device_number: 0
      value: 7
  - delay: 2
  mode: single
- id: '1628220785067'
  alias: Pool Colour Blue
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_light_colour
    to: Blue
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 7
      device_number: 0
      value: 5
  - delay: 2
  mode: single
- id: '1628221525177'
  alias: Pool Colour Red
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_light_colour
    to: Red
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 7
      device_number: 0
      value: 1
  - delay: 2
  mode: single
- id: '1628221656195'
  alias: Pool Colour Disco
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_light_colour
    to: Disco
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 7
      device_number: 0
      value: 10
  - delay: 2
  mode: single
- id: '1628341866750'
  alias: Pool Favourite - All Auto
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_set_fave
    to: All_Auto
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 8
      device_number: 255
  - delay: 2
  mode: single
- id: '1631143453414'
  alias: Pool Favourite - All Off
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_set_fave
    to: All_Off
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 8
      device_number: 128
  - delay: 2
  mode: single
- id: '1631143453413'
  alias: Pool Favourite - Daily Filter
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.pool_set_fave
    to: Daily_Filter
  condition: []
  action:
  - service: rest_command.poolaction
    data:
      action_code: 8
      device_number: 2
  - delay: 2
  mode: single

FWIW All the action/device/value codes are in the API docs. You can find out what number the specific favourites are by selecting them on the controller and then seeing what shows up in HA as the current “Active Favourite”. Finally, the missing bit is the pH and ORP data. This is currently not exposed in the API so until that changes, you can either ignore it or try grabbing it via a scrape tool. I’ve used parsehub to do this, but am finding that it doesn’t seem to run reliably - I suspect it may be throttled as to how many calls you can make and I’m hitting that. It’s also a little annoying in that (unless you pay for a subscription) you have to send a command to run the scrape then a separate command to pull the results down. I’m currently playing with ‘multiscrape’ (available via HACS) - looks promising, although I’m being challenged by the login process at the moment.

Cheers

Wow thank you so much for all this effort, this is super helpful!

I know you can add things to the Configuration.yaml either via SMB file explorer or when I install a package from HACS it seems that you can do it through the lovelace interface.

When you say here add it to the config.yaml, where am I doing it? And then once I do that, will it create all the entities automatically - like sensor.pool_status?

Subsequently, when I go to my dashboard > edit > add component > ‘gauge’ as an example, I should just be able to select the sensor.pool_status for example?

Thank you!

Shelly 1PM

OK. From all this I can see you are just starting down the rabbit hole of HA, which is great to see. Welcome to the club! Main thing is to do it in stages, and always have backups.

Firstly, yes you can edit the files directly with a text editor of your choice by setting up smb, or by using an add on via HACS. You can also edit it from within HA itself: click on “file editor” on the left hand side of the Lovelace interface and choose the file to edit - this of course lacks some features of a full editor but is quick to use. However you do it, make sure you always do the following three things:

  • Have a backup copy of any file you are changing, so you can go back if necessary
  • Only make small changes at any one time - it’s easier to troubleshoot if there is an error
  • After making any change, always test to make sure you haven’t broken anything (configurations/server controls/check configuration) before restarting the server

As an aside, it is also a good idea to have a recent whole server backup somewhere - supervisor/backups/create backup, then click on the most recent backup, click on the three dots, and download the backup locally - if you ever have to start from scratch, you can then always restore this working version of your server.

To get back on topic, the files we are changing here are configuration.yaml (this is a critical one - breaking this could do bad things up to and including stopping your server from starting), and automations.yaml (less critical, but of course still important). Note that you definitely can create all the automations via the web gui, but it can be confusing - personally I find the gui easier to make changes to existing automations rather than to create new ones. In any case I’d suggest creating at least one directly, make sure you give it a unique id (this enables you to edit it via the gui later), and then you know what settings to use in the gui from then on if you want to use that to create more.

Again - one step at a time - I’d recommend firstly just getting the sensors visible by adding in the first two blocks of code (the - platform: rest, and - platform: template). Then you will be able to add those sensors in to lovelace via “add card” then “entities” then selecting the new sensors. Note that after changing anything in the configuration.yaml file you need to restart the server (configuration/server controls/server management/check configuration then configuration/server controls/server management/restart) for them to be loaded. After changing anything in the automations, if you do it via the gui (configuration/automations) it is sanity checked then - if ok - applied immediately on saving the change. If you do it via an editor then you need to go to configuration/server controls/server management/check configuration and then configuration/server controls and click once on ‘automations’.

Have fun!

I’d love to use the Shelly to monitor the power on my heat pump, but I have a monster that can suck over 24 amps so that would fry the Shelly which can “only” handle up to 16 amps (which is of course more than enough for most things). For my purposes I need something like the https://aeotec.com/outdoor-z-wave-switch/ that can handle up to 40 amps - I do have Zwave devices already so it’s on my wish list. :wink:

If its just current montioring, you could also look at a non-invasive current clamp connected to ESP8266 or ESP32

e.g.
https://www.alibaba.com/product-detail/wireless-wifi-power-meter-CT-sensor_60634160945.html?spm=a2700.7724857.normal_offer.d_title.6a614452PEdPNK

Excellent point. Not as accurate, but good enough. I have a number of those things laying around (the D1 mini is my favourite) so I’ll definitely have a look at that. I even have a few clamps from when I changed from using them to measure power usage, to getting it straight from the smart meter via zigbee…

Wow thanks so much for this - this is incredible context. The main thing that I wasn’t understanding is that I am copying all this code into the config.yaml and its go not real structure to it - i.e. I have some lines in there for the Nest Cams, I have lines in there for homekit, lines for climate (my gas fireplaces)… does it matter how its all represented or do I just keep adding to the list?

Also, I tried adding the platform: rest and I got
Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/configuration.yaml”, line 33, column 9

I assume thats the indenting - is there an easy way to know what is meant to be indented which amount?
And can I leave empty lines between code to give it some structure?

Indenting is critical with yaml - again, that’s why even after doing this for a while it’s a good idea to only make small changes as it’s easier to pick out what is wrong. Over time you also start to get a feel of it. There are text editors out there you can use that help - personally I go old school, but then again I don’t do enough to warrant bothering with anything else.

Yes, you can keep adding to the list, but it can get messy. To make the code readable - the best thing to do is use the “#” character in front of anything you want to be ignored. This is a good way of testing code as well. Many people also create a block of text, commented out with the “#”, to break up the code. You can also create separate files of yaml code and include that into the main code - handy when a file starts getting too big.

Cheers

Thank you! I got it working by removing all indentations.

Glad to hear it’s working. Just to let you know, yaml generally uses two spaces to group things. Think of it like the title of a chapter, and subtitles, and content. To that end, although what you have there might be working now, it may also cause some issues down the line by confusing anything you put after it. For example, I have my sensors grouped together so I have:

sensors:
# Uptime sensor
  - platform: uptime
    name: Time Online
# Connectmypool Rest sensor
  - platform: rest
    name: Pool Status
    resource: https://www.connectmypool.com.au/api/poolstatus
    method: POST
    payload: '{ "pool_api_code": "change this" }'
    force_update: true
    json_attributes:
    - pool_spa_selection
    - heat_cool_selection
    - temperature
    - active_favourite
    - heaters
    - solar_systems
    - channels
    - valves
    - lighting_zones
    value_template: 'OK'
    scan_interval: 120
    verify_ssl: true
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json 
# Template sensors
  - platform: template
    sensors:
      pool1_heat_cool_selection:
        friendly_name: Pool - Heat Cool Selection
        value_template:   >-
          {{ ['Cooling', 'Heating'][states.sensor.pool_status.attributes["heat_cool_selection"]|int] }} 

I’ve added a few more things in there to help give you the idea.

You mentioned in your original post above - "Optionally, you can do what I did which is to use input_select to make a nice drop down - you mention it is time consuming, but I wasn’t sure why since I just copied the code into my YAML but I am sure I have done something wrong. More on that below.

I have been able to make some headway but I am not sure if what is being shown in HA is just because of the code or it is actually reading from the API. Reason is because I added the below code to my config.yaml - added it to a card but whatever I do, the pool does not respond.

So how does the input_select link back to the config.yaml - i.e. how does it know that pool_lights_mode links to the pool lights since the sensor has a different name?

input_select:
  pool_lights_mode:
    name: Pool Lights Mode
    options:
      - "off"
      - "auto"
      - "on"
    icon: mdi:dome-light
  pool_light_colour:
    name: Pool Lights Colour
    options:
      - "Ocean"
      - "Red"
      - "White"
      - "Blue"
      - "Disco"
    icon: mdi:palette
  pool_set_temp:
    name: Pool Set Temp
    options:
      - "18"
      - "28"
      - "38"
    icon: mdi:coolant-temperature
  pool_set_fave:
    name: Pool Set Favourite
    options:
      - "Daily_Filter"
      - "All_Off"
      - "All_Auto"
    icon: mdi:heart-plus-outline

I tried to add my additional channels and sensors but I am not sure I have done it right - looking at yours, your main pump channel wa s0 but I don’t have a 0, I only have a 1. Could you let me know if I have done it correctly?

Note: Not sure why it says FALSE for lighting zones coloured - because they are definitely coloured.

Lastly, your Automations.yaml - what does that actually do? I noticed you said that you used ‘random ID’s’, why did you do that? Do I need to generate some or can I just use the ones you have?

So, time consuming because you have to put in multiple entries in the yaml configuration file and then match that in the automations file. Obviously it would be less time consuming for you if you have just copy and pasted what I had done!

The input_select in configuration.yaml creates an entity called “input_select.pool_lights_mode” with the attribute set to whatever you choose from the drop down list - let’s say for example “off”. The separate automation script in automation.yaml then performs an action based on this - when the entity changes to “off” then it fires off the rest command to turn the lights off.

Your pool and mine will be different. As such, you may/will need to modify the sensors that I have put into the example code to match yours. To work out what they are, go into developer tools and look at the properties of “sensor.pool_status”. A good way to confirm if you have done things correctly is to open up the “connectmypool” website on your laptop, or via the app, then change something via HA and confirm it’s doing what it’s supposed to be doing.

Not sure what you mean by that - but again it is quite likely that your pool doesn’t match mine, so I’m afraid you need to work that out yourself as per above.

See above, but basically it is used to send rest commands based on selections made from the drop down lists. The IDs can be anything you want, and yes you can use the ones in mine, as long as they don’t clash with something else already in there. You can live without them, but if they are there then you will be able to make changes to them from within the Lovelace interface later, rather than having to muck around with the code directly. If you want you can always create a basic automation via Lovelace, and then go into the file and use the ID that was created that way… or you can think of a favorite number like pi, and just change the last few digits… again, it doesn’t really matter, other than making sure they don’t clash.

Thanks mate - please PM me your address so I can send you a case of Beer - or Wine, in case you don’t drink Beer :slight_smile:

1 Like

I drink both, and the offer is definitely appreciated, but not needed at all - just glad to help. I know that in the past I’ve benefitted from the advice/experience/knowledge of others so think that it is important to ‘pay it back’ when possible.

I also run a small IT support business so if you like I can ping you the details for that, as - as well as getting people’s printers to work - I’m also happy to provide support for Home Automation installations with experience ranging from Philips Hue, Zigbee, Zwave, Vera, Home Assistant, esphome, Tuya, Xiaomi, Homekit, Google, HomeGenie, IFTTT, openHAB… I actually started rolling my own home automation many many years ago, writing scripts on linux, then started using X10 kit, wrote some code for misterhouse. Gotta say I love HA compared to some of the alternatives - but yaml really did my head in for quite a while. :wink:

Cheers