Smart-me switch and sensor with rest api

Hi everybody,

i try to use my smart-me switch which has a rest-api:
https://smart-me.com/swagger/ui/index#!/Actions/Actions_Post

i got so far to use the sensor completly

sensor:
  - platform: rest
    name: smartmesensor
    json_attributes:
        - Id
        - Name
        - Serial
        - DeviceEnergyType
        - FamilyType
        - ActivePower 
        - ActivePowerUnit
        - CounterReading
        - CounterReadingUnit
        - CounterReadingT1
        - CounterReadingImport
        - SwitchON
        - Voltage
        - VoltageL1
        - Current
        - PowerFactor
        - PowerFactorL1
        - Temperature
        - ValueDate
    resource: https://smart-me.com:443/api/Devices/***DEVICE-ID***
    authentication: basic
    username: ***my_username***
    password: ***my_passwort***
    value_template: 'ok'
  - platform: template
    sensors:
      sm_activepower:
        value_template: '{{states.sensor.smartmesensor.attributes["ActivePower"] }}'
        device_class: power
        unit_of_measurement: 'kWh'
      sm_voltage:
        value_template: '{{states.sensor.smartmesensor.attributes["Voltage"] }}'
        unit_of_measurement: 'V'
      sm_counterreading:
        value_template: '{{states.sensor.smartmesensor.attributes["CounterReading"] }}'
        device_class: power
        unit_of_measurement: 'kWh'
      sm_current:
        value_template: '{{states.sensor.smartmesensor.attributes["Current"] }}'
        unit_of_measurement: 'A'
      sm_switchon:
        value_template: '{{states.sensor.smartmesensor.attributes["SwitchON"] }}'

But i cannot get the switch functionality to work:

switch:
  - platform: rest
    name: smartme
    resource: https://smart-me.com:443/api/Actions
    username: ***username***
    password: ***passwort
    body_on:  '{"DeviceID": "***device-id****","Actions": [{"ObisCode": "***obiscode***","Value": 1}]}'
    body_off: '{"DeviceID": "***device-id***","Actions": [{"ObisCode": "***obiscode***","Value": 0}]}'
    headers:
      Content-Type: application/json
      authentication: basic

with the message being:

{
  "DeviceID": "string",
  "Actions": [
    {
      "ObisCode": "string",
      "Value": 0
    }
  ]
}

i get the following error about the switch function:
2020-04-03 21:08:17 ERROR (MainThread) [homeassistant.components.rest.switch] Got non-ok response from resource: 405

Thanks for helping

By specifying username/password in the config, it will already do the authentication:basic for you. It might be messing it up with you specifying it again in the headers section. Especially since the header would need the password encoded following it.

Just a guess.

Also, I recommend using the secrets file. Especially for cases like this :slight_smile:

Then, your config would just look like this (no need to filter out uname/password anymore)

switch:
  - platform: rest
    name: smartme
    resource: https://smart-me.com:443/api/Actions
    username: !secret my_smartme_username
    password: !secret my_smartme_password
    ...

Thanks for your help. I tried your advise. First of all the !secret feature is very cool.
but with the following switch function i still get an error code:

  - platform: rest
    name: smartme
    resource: https://smart-me.com:443/api/Actions
    username: !secret smartme-username
    password: !secret smartme-password
    body_on:  '{"DeviceID": "***DeviceID***","Actions": [{"ObisCode": "***obiscode***","Value": 1}]}'
    body_off: '{"DeviceID": "***DeviceID***","Actions": [{"ObisCode": "***obiscode***","Value": 0}]}'

Error code from home-assistant.log

2020-04-04 01:39:53 ERROR (MainThread) [homeassistant.components.rest.switch] Got non-ok response from resource: 405

I’d still keep the other header in there

headers:
      Content-Type: application/json

405 error is a tough one to solve. I’m just hoping it’s with our request and not content. I don’t have an account or api key, so I cant test.

If you paste your body_on directly to their form there for /api/Actions/ POST, it works? You’ll have to log in first (I get auth errors when I try).

Thanks so much for your quick help! i tried it with the other header as well. But unfortunately it got me the same result.

When i add the body_on command in their api-form it works and gets a proper result and my switch works.

I also got it to work with curl:

curl -v--user username:passwort -H "Content-Type: application/json" -d '{"DeviceID": "***DEVICE-ID","Actions": [{"ObisCode": "***OBISCODE","Value": 1}]}' https://smart-me.com:443/api/Actions

Oh, the curl command works. That’s good!

Maybe DeviceID or obiscode have some special characters in it??? Not sure. Since curl command works, we’re basically down to the actual payloads.

Hi Jim,
i got it working with the command_line switches command:

  - platform: command_line
    switches:
      coffeemachine:
        command_on: >-
          curl -v --user username:passwort -H "Content-Type: application/json" -d '{"DeviceID": "***DeviceID***","Actions": [{"ObisCode": "***ObisCode***","Value": 1}]}' https://smart-me.com:443/api/Actions
        command_off: >-
          curl -v --user username:passwort -H "Content-Type: application/json" -d '{"DeviceID": "***DeviceID***","Actions": [{"ObisCode": "***ObisCode***","Value": 0}]}' https://smart-me.com:443/api/Actions
2 Likes

Hey

Thx for sharing your progress with the Smart-Me Switch.

I was trying to add an additional curl request in the command_state and jquery for the SwitchOn value in the json array so Home Assistant would not offer an on and off switch.

I am pretty new to all this and seem to miss something :blush:

  - platform: command_line
    switches:
      smartmeswitch:
        command_on: >-
          curl -v --user user:password -H "Content-Type: application/json" -d '{"DeviceID": "xxx","Actions": [{"ObisCode": "xxx","Value": 1}]}' https://smart-me.com:443/api/Actions
        command_off: >-
          curl -v --user user:password -H "Content-Type: application/json" -d '{"DeviceID": "xxx","Actions": [{"ObisCode": "xxx","Value": 0}]}' https://smart-me.com:443/api/Actions
        command_state: >- 
          curl -X GET --user user:password -H "Content-Type: application/json" "https://smart-me.com:443/api/Devices/xxx" | jq ".SwitchOn"
        value_template: '{{ return == "true" }}' 

Thx and regards,

Dom

I don’t think you can access the return code. Actually, I think the variable “value” will be the return code.

Does your command return literal “true” on success? Either way, the special variables used for most all rest/command/etc things are ‘value’, or ‘value_json’ (which just attempts to return ‘value’ as a json object instead of a string.

value_template: '{{ value == "true" }}'

thx for your answer. I found another way to make it work with the sensor where SwitchOn Value was allready readable. Even if the representation switch on the dashboard acts a little slow. Still better instead of having On and Off Switch separated :slightly_smiling

        command_state: 'true'
        value_template: "{{ states('sensor.smartmeterswitchon') }}"

Regards,

Dom

Ha, that works too!

But value_template is redundant in this case. The state of the sensor is the command_state…unless you specify value_template. Then, it will take the command_state, and run it through the template.

Unless ‘sensor.smartmeterswitchon’ is a different switch completely, then disregard! Your solution would be correct in that case!

hmmm this is confusing. The command_line how to says that I only can use the state when the value would be 0 (but for Smart-Me it is true or false. Therefore I added the value_template and just gave the command_state a valid value which would be ignored because of the value_template anyway :thinking:

command_state
(string)(Optional)

If given, this command will be run. Returning a result code 0 will indicate that the switch is on.

value_template
(string)(Optional)

If specified, command_state will ignore the result code of the command but the template evaluating to true will indicate the switch is on.

PS: I only have on SmartMe Switch which I also use as the sensor.

You might have missed my edit. I think I assumed ‘sensor.smartmeterswitchon’ was the name of this command line switch.

So yeah, give it a dummy command to run and specify a value_template looking at another thing. That will work just fine!! …I think!

By doing it your way, your state changes are going to be delayed by the polling interval. You’ll have to wait until this integration decides to run the command_state command again to read the sensor value. Luckily, whenever you toggle this switch, it forces an update. So you might not notice it until you try to control this switch from another source that isn’t this integration. But if there is no way to turn this light on otherwise (physical wall button), you’ll never even notice!

If you do notice, or want to change it, A template switch will be able to be update realtime.

switch:
  - platform: template
    switches:
      smart_me:
        # State of this switch follows this sensor
        value_template: "{{ states('sensor.smartmeterswitchon') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.smartmeswitch
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.smartmeswitch

Now you have a switch that runs the right on/off commands and has a state value that is update IMMEDIATELY when sensor.smartmeterswitchon is updated from anywhere.

You won’t notice the delay until you try to control the smartme switch from another method. If you only ever use the Home Assistant switch, it will update the state right away.

1 Like

Thanks so much! That works for me too!

I never became happy with my smartme setup because I always had this shorty flipping of the switch whenever turning on or off.

As I made some very positive experiences with the input_boolean switch I decided to give it a shot. I created an input_boolean switch and used the existing Rest Sensor to determine if the original smartme app has been used to toggle the switch. I kept my command line switch but I leave the state update to the input_boolean.

#configuration.yaml

input_boolean:
  notify_smartme_switch_pool_pumpe:
    name: smartme_switch
    icon: mdi:switch

# SmartMe Switch Status Sensor
 - platform: rest
   name: SmartMeterSwitchOn
   json_attributes:
    SwitchOn
   resource: https://smart-me.com:443/api/Devices/*****
   username: ****
   password: *****
   authentication: basic
   value_template: '{{ value_json.SwitchOn }}'
   scan_interval: 10
   headers:
    Content-Type: application/json
    User-Agent: Home Assistant REST sensor

#Smart-Me command line Switch
switch:
 - platform: command_line
   switches:
     smartmeswitch:
       command_on: >-
         curl -v --user user:pw -H "Content-Type: application/json" -d '{"DeviceID": "***","Actions": [{"ObisCode": "63000C0001FF","Value": 1}]}' https://smart-me.com:443/api/Actions
       command_off: >-
         curl -v --user user:pw -H "Content-Type: application/json" -d '{"DeviceID": "***","Actions": [{"ObisCode": "63000C0001FF","Value": 0}]}' https://smart-me.com:443/api/Actions

#automations.yaml

- id: SmartMe Switch Boolean API from on to off
  alias: 'Smart-Me Switch api toggles from on to off'
  trigger:
  - entity_id: sensor.smartmeterswitchon
    from: 'True'
    platform: state
    to: 'False'
  action:
  - entity_id: input_boolean.notify_smartme_switch_pool_pumpe
    service: input_boolean.turn_off
- id: SmartMe Switch Boolean API from off to on
  alias: 'Smart-Me Switch api toggles from off to on'
  trigger:
  - entity_id: sensor.smartmeterswitchon
    from: 'False'
    platform: state
    to: 'True'
  action:
  - entity_id: input_boolean.notify_smartme_switch_pool_pumpe
    service: input_boolean.turn_on
- id: SmartMe switch on to off based on input boolean
  alias: 'SmartMe switch on to off based on input boolean'
  trigger:
  - entity_id: input_boolean.notify_smartme_switch_pool_pumpe
    from: 'on'
    platform: state
    to: 'off'
  action:
  - entity_id: switch.smartmeswitch
    service: switch.turn_off
- id: SmartMe switch off to on based on input boolean
  alias: 'SmartMe switch off to on based on input boolean'
  trigger:
  - entity_id: input_boolean.notify_smartme_switch_pool_pumpe
    from: 'off'
    platform: state
    to: 'on'
  action:
  - entity_id: switch.smartmeswitch
    service: switch.turn_on

I only use the input boolean switch to switch the state from HA. This way the state is always up to date and doesn’t flip to the wrong state for a second.

Have a great day

Regards,

Doemer

I finally found the solution to use the switches on Smart-Me. On their API there’s one resource to SET the action and another resource to GET the state of the device. —> use"state_resource"

Here’s the code, just make sure to use the correct device-ID (3 times):

switch:
  - platform: rest
    name: SmartMe DigitalOutput2
    username: !secret smart_me_username
    password: !secret smart_me_passwort
    resource: https://smart-me.com:443/api/Actions
    state_resource: https://smart-me.com:443/api/Devices/***DEVICE-ID***
    body_on:  '{"DeviceID": "***DEVICE-ID***", "Actions": [{"ObisCode": "63000C0102FF","Value": 1}]}'
    body_off: '{"DeviceID": "***DEVICE-ID***", "Actions": [{"ObisCode": "63000C0102FF","Value": 0}]}'
    is_on_template: "{{ value_json.DigitalOutput2 }}"
    headers:
      User-Agent: Homeassistant REST
      Content-Type: application/json
    verify_ssl: true  #optional

reminder:

  • use ObisCode 63000C0101FF to get DigitalOutput1
  • use ObisCode 63000C0102FF to get DigitalOutput2
  • and if you’re silly enough to buy the overpriced smart-me-plug:
    use ObisCode 63000C0101FF to get SwitchOn

Additional reminder (for whoever ends up on this topic using google):
You could get the DeviceID on the Smart-Me’s API-Website, try the GET /api/Devices: smart-me REST API
Don’t forget to insert your login at the top-right corner.

There’s only one ugly thing I could not solve yet: If you toggle the switch on homeassistant, it does not wait for the action to be set and updates the “state_resource” instantly. After that, your switch on homeassistant drops back to the initial state for about 30 sec, until it gets finally updated by the correct state. I don’t see any option to set a delay on the REST-switches :frowning:

Hi Include

Does this setup work with the newly added Energy dashboard in HA?

Thanks